Resources
Basis
HEAD
HEAD
is the location in the commit tree. It's a "pointer" to a commit. It is possible to move it with git checkout
.
Config
git config --listParameters list.
git config user.nameDisplay a parameter (
user.name
here).git config --global user.name myNameChange parameter value.
git config --global gpg.format sshSet SSH key method 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.
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.
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 (
-S
to sign the commit and-m
to set the commit's message).git commit --amendModify last commit (only works if commit was not pushed).
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 myBranchMove HEAD to last commit of myBranch.
git checkout myBranch^Move HEAD to penultimate commit of myBranch.
git checkout HEAD^Moves HEAD 1 commit back from current HEAD.
git checkout HEAD^^^Moves HEAD 3 commits back from current HEAD.
git checkout HEAD~12Moves HEAD 12 commits back from current HEAD.
git checkout HEAD^Moves HEAD 1 commit back from current HEAD in the 1st parent of a merge.
git checkout HEAD^2Moves HEAD 1 commit back from current HEAD in the 2nd parent of a merge.
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 (
origin
is 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 Tools
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.
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-submodule
to 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
.
Commit History
Log
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 local history with IDs.
History Rewrite
git reset HEAD^Moves HEAD 1 commit back as if it had never happened (can break things if the commit as laeready been pushed to remote).
git revert HEAD^Create new commit that undo last commit changes.
git cherry-pick commit1 commit12 ccommit7Add commits from other branches after HEAD.
git rebase myBranchTakes commits on myBranch and replays them on current branch (from their last common parent).
git rebase --rootTakes commits from the root of the repository and replays them.
git rebase --committer-date-is-author-date --rootRebase commits from the root and keep original commit date.
git rebase -i myBranchUse rebase interactive mode. Select commits that you want to modify by changing
pick
bye
(for edit) and reorder commits as you which.git commit --amend --no-editMake your modification to the code and then issue this command to modify the commit (
--no-edit
to keep the name of the old commit).git rebase --continueContinue to the next commit marked for edition.
git rebase --abortAbort the rebase (useful in case of issues).