Resources
- Learn Git Branching
- Oh Shit Git
- How Git Actually Works - LeanThatStack
- The difference between the working directory and the repository - Anna Skoulikari
Concepts
HEAD
HEADis the location in the commit tree. It's a "pointer" to a commit.HEADcan be moved usinggit checkout.- If
HEADpoints to a raw commit (not a branch), the branch is indetachedstate. - If new commits are created from a
detachedpoints, those commits will be orphaned (and they will be deleted by garbage collector).
Areas
- Working Directory: Actual files on the disk.
- Staging Area (also called Index): Area to prepare which file will be included in next commit.
- Local Repository: The database of commits.
- Remote Repository: Remote save of the Repository.
- Stash: Space to temporary save changes without having to commit.
Simplified Workflows:
- Working Directory >
git add> Staging Area >git commit> Local Repository >git push> Remote Repository - Working Directory >
git stash> Stash
Basis
Workflow
git initCreate git repository (local).
git statusShow status files (modified, added, removed, staged, ...).
git add myFileStage myFile (prepare for commit).
git add -AStage all files (including untracked/new ones).
git commit -S -m "Commit Message"Create a commit (
-Sto sign the commit and-mto set the commit's message).git commit --amendModify last commit.Warning: will create a conflict if the commit was previously pushed to remote.
Stash
git stash listList stashes.
git stash save myStashNamePut changes in a side working directory for later use.
git stash apply stashIndexAdd stashed code back into the code.
git stash drop stashIndexDelete stash.
Commit History
git show myCommitDisplay commit information.
git logShow commit history.
git log --pretty=fullerCommit history with more info (Committer name, commit date,...).
git log --show-signatureShow signature information on commit history.
git log --oneline --graph --decorateShow commit history with graph.
git log -pShows difference between commits.
git reflogShow recent local history with commit IDs.Note: reflog entries expires (90d for reachable commits, 30d for orphaned commits).
Branching
Branch
git branchShow local branches.
git branch -rShow remote branches.
git branch myBranchCreate new branch.
git merge myBranchMerge myBranch to current branch.
Checkout
git checkout HEAD^Moves HEAD to 1 commit back from current HEAD.
git checkout HEAD^^^Moves HEAD to 3 commits back from current HEAD.
git checkout HEAD~12Moves HEAD to 12 commits back from current HEAD.
git checkout HEAD^Moves HEAD to 1 commit back from current HEAD in the 1st parent of a merge.
git checkout HEAD^2Moves HEAD to 1 commit back from current HEAD in the 2nd parent of a merge.
git checkout myBranchMove HEAD to last commit of myBranch.
git checkout myBranch^Move HEAD to penultimate commit of myBranch.
Tag
git tag myTagCreate a tag.
git tag -d myTaggit push --delete origin myTagDelete a tag.
Remote
Remote Workflow
git clone [REPO_URL]Clone remote repo to local machine.
git pushPush commits to remote repo.
git fetchGet last changes from the remote repo.
git pullGet last changes from the remote repo and merge them with local.
Remote Branch
git remote -vDisplay Remote Branches and their URL.
git remote add origin git@github.com:myUser/myRepo.gitAdd Remote Branch (
originis the name of the branch, it can be anything).git remote set-url origin git@github.com:myUser/myOtherRepo.gitUpdate URL for the Remote Branch.
git remote remove originRemove Remote Branch.
git branch myBranch --set-upstream-to=origin/myBranchSet Upstream for a Branch.
Git Config
Basis
git config --listParameters list.
git config user.nameDisplay a parameter (
user.namehere).git config --global user.name myNameChange parameter value.
Credentials Helpers
git config --global credential.helper cacheSave credentials in memory.
git config --global credential.helper storeSave credentials in plaintext in
~/.git-credentials(less secure).git config --global credential.helper osxkeychainSave credentials in MacOS Keychain (more secure).
git config --global credential.helper managerSave credentials in Windows Keychain (more secure).
Commit Signing
git config --global gpg.format sshSet SSH key format to sign commits.
git config --global user.signingkey /path/to/my/public/keySpecify the public key to used for signing commits (the private key has to be configured in GitHub settings.
SSL
git config --global http.sslBackend schannelSwitch SSL Backend from OpenSSL (default) to Schannel (Windows built-in). This is useful in an organization with enterprise-managed certificates.
Git Tools
Submodule
git submodule statusShow if any submodules are configured in the current repo. A submodule is a repo in a repo.
git add submodule [SUBMODULE_REPO_URL]Add a submodule . Use
--recurse-submoduleto include submodule when using git commands.git submodule update --init --remote --rebaseUpdate submodule version (use the last commit of the submodule).
git clone --recurse-submoduleClone repo and it's submodules.
LFS
git count-objects -vHShow size of the repo. Not directly related to LFS but cans be usefull.
git lfs ls-files -sShow LFS files and their size on current branch.
git lfs ls-files -aShow LFS files on the full history (after a file is uploaded on LFS it will still exist on the remote storage even if its reference is removed in the HEAD.
git lfs installgit lfs track "*.png"git add .gitattributesAdd LFS to the repo (will track all new PNG files added to the repo).
git lfs statusShow staged files.
git lfs untrack "*.png"git add --renormalize .git commit -m 'Restore file that were previously in LFS'git pushRevert files from being on LFS. Do not remove
.gitattributes, it is required to rungit add --renormalize.
Rewrite History
Reset / Revert
git reset --soft HEAD^Moves current branch to one commit back and puts changes in Staging.Warning: will create a conflict if the commit was previously pushed to remote.
git reset --mixed HEAD^Moves current branch to one commit back and puts changes from the commit in Working Directory.Warning: will create a conflict if the commit was previously pushed to remote.
git reset --hard HEAD^Moves current branch to one commit back and discard all changes (this also removes any unstaged files that were not part of the commit).Warning: will create a conflict if the commit was previously pushed to remote.
git revert HEAD^Create new commit that undo last commit changes.
Cherry Pick
git cherry-pick commit1 commit12 ccommit7Add commits from other branches after HEAD.
Rebase
git rebase myBranchReplays current branch commits on myBranch (from their last common parent).
git rebase --rootReplays all commits from the root of the repository.
git rebase --root --committer-date-is-author-dateReplays all commits from the root and keep original commit date.
git rebase -i myBranchUse rebase in interactive mode. Select commits to keep (
pick), to modify (edit), squash commits (squash), reorder commits, ...git rebase --continueContinue to the next commit marked for edition.
git commit --amend --no-editMake your modification to the code and then issue this command to modify the commit (
--no-editto keep the name of the old commit).git rebase --abortAbort the rebase (useful in case of issues).
git rebase -i $(git merge-base myBranch HEAD)Rebase from the point it diverged from myBranch (first commit on current branch). Useful for squashing commit before a merge.
