Git cheatsheet
Some commands for git I found useful.
Log
Check history of single line (or range of lines)
git log -L15,+1:'path/to/your/file.txt'
# Detailed description
# -L <start>,<end>:<file>
# -L :<funcname>:<file>
Graph view
git log --oneline --graph
git log --oneline --decorate --all --graph
Checkout
Create branch and checkout to it
git checkout -b <branchname>
Checkout and merge simultaneously
git checkout -m <branchname>
Reset
Warning: Do not use reset
commands! As alternative to git reset --hard
use git stash -u
to stash all files (untracked included), so it can be reverted later.
git reset
list
git reset --soft # No changes in index/working tree
git reset --mixed # Changes index, but not working tree
git reset --hard # Reset index and working tree
Discard unstaged changes for a file
git checkout "path/to/file"
Detaching HEAD
git checkout --detach [<branch>]
git checkout [--detach] <commit>
Detaching HEAD, leading to “DETACHED HEAD” state. “DETACHED HEAD” - HEAD usually points to tip of some branch, but if we use git checkout –detach - we can point it to some commit. In this state we can create commits from another commit, but these commits WON’T BE POINTED BY SOMEONE!
Nothing points here, these commits will be deleted by
garbage collector
|
v HEAD (refers to branch 'master')
e---f |
/ v
a---b---c---d branch 'master' (refers to commit 'd')
^
|
tag 'v2.0' (refers to commit 'b')
Use:
git checkout -b foo # or "git switch -c foo" **(1)**
git branch foo **(2)**
git tag foo **(3)**
Clone
Clone to specified folder
git clone <dest> <folder>
Clone current repository (only files, tracked by git) to other folder. Useful, when you want to prepare files for transfer (so it won’t include cache, build files and all other stuff that should be in your .gitignore
)
# Inside of repository
git clone . <path/to/folder>
Squash
Squash last N commits (interactive) through rebase
git rebase -i HEAD~<N>
Tagging
Tags are very useful to mark specific commits. It’s more lightweight than creating whole new branch.
git tag -a v1.4 -m "my version 1.4"
# Such tags won't be pushed automatically, do it like this:
git push origin v1.4
Remove tracking files
Such action is often needed, if gitignore
is updated with path to file, that is already being tracked.
git rm --cached <file> # Remove file from tracking, but leave it in index
Handling remotes
Rename remote
git remote rename origin old-origin
git remote add origin https://dev.robopro.pro/...
Push all tags and branches to remote: (can work not very well)
git push --set-upstream origin --all
git push --set-upstream origin --tags
Configuration
git config --global user.name
git config --global user.email
git config --global credential.helper 'cache --timeout 900'
# 15 minutes. Might not be safe
Empty branch
Use the --orphan
when creating the branch:
git checkout --orphan YourBranchName
This branch may be unpushable (to make it pushable, create commit with following command)
git commit --allow-empty -m "Initial commit"