Resources

Basis


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 --list
Parameters list.

git config user.name
Display a parameter (user.name here).

git config --global user.name myName
Change parameter value.

git config --global gpg.format ssh
Set SSH key method to sign commits.

git config --global user.signingkey /path/to/my/public/key
Specify the public key to used for signing commits (the private key has to be configured in GitHub settings.

git config --global http.sslBackend schannel
Switch SSL Backend from OpenSSL (default) to Schannel (Windows built-in). This is usefulĀ in an organization with enterprise-managed certificates.


Workflow

git init
Create git repository (local).

git status
Show status files (modified, added, removed, staged, ...).

git add myFile
Stage myFile (prepare for commit).

git add -A
Stage 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 --amend
Modify last commit (only works if commit was not pushed).

Branching


Branch

git branch
Show local branches.

git branch -r
Show remote branches.

git branch myBranch
Create new branch.

git merge myBranch
Merge myBranch to current branch.


Checkout

git checkout myBranch
Move 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~12
Moves 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^2
Moves HEAD 1 commit back from current HEAD in the 2nd parent of a merge.


Tag

git tag myTag
Create a tag.

git tag -d myTag
git push --delete origin myTag
Delete a tag.

Remote


Remote Workflow

git clone [REPO_URL]
Clone remote repo to local machine.

git push
Push commits to remote repo.

git fetch
Get last changes from the remote repo.

git pull
Get last changes from the remote repo and merge them with local.


Remote Branch

git remote -v
Display Remote Branches and their URL.

git remote add origin git@github.com:myUser/myRepo.git
Add Remote Branch (origin is the name of the branch, it can be anything).

git remote set-url origin git@github.com:myUser/myOtherRepo.git
Update URL for the Remote Branch.

git remote remove origin
Remove Remote Branch.

git branch myBranch --set-upstream-to=origin/myBranch
Set Upstream for a Branch.

Git Tools


Stash

git stash list
List stashes.

git stash save myStashName
Put changes in a side working directory for later use.

git stash apply stashIndex
Add stashed code back into the code.

git stash drop stashIndex
Delete stash.


Submodule

git submodule status
Show 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 --rebase
Update submodule version (use the last commit of the submodule).

git clone --recurse-submodule
Clone repo and it's submodules.


LFS

git count-objects -vH
Show size of the repo. Not directly related to LFS but cans be usefull.

git lfs ls-files -s
Show LFS files and their size on current branch.

git lfs ls-files -a
Show 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 install
git lfs track "*.png"
git add .gitattributes
Add LFS to the repo (will track all new PNG files added to the repo).

git lfs status
Show staged files.

git lfs untrack "*.png"
git add --renormalize .
git commit -m 'Restore file that were previously in LFS'
git push
Revert files from being on LFS. Do not remove .gitattributes, it is required to run git add --renormalize.

Commit History


Log

git show myCommit
Display commit information.

git log
Show commit history.

git log --pretty=fuller
Commit history with more info (Committer name, commit date,...).

git log --show-signature
Show signature information on commit history.

git log --oneline --graph --decorate
Show commit history with graph.

git log -p
Shows difference between commits.

git reflog
Show 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 ccommit7
Add commits from other branches after HEAD.

git rebase myBranch
Takes commits on myBranch and replays them on current branch (from their last common parent).

git rebase --root
Takes commits from the root of the repository and replays them.

git rebase --committer-date-is-author-date --root
Rebase commits from the root and keep original commit date.

git rebase -i myBranch
Use rebase interactive mode. Select commits that you want to modify by changing pick by e (for edit) and reorder commits as you which.

git commit --amend --no-edit
Make 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 --continue
Continue to the next commit marked for edition.

git rebase --abort
Abort the rebase (useful in case of issues).