Git Cheat Sheet
Git version control cheat sheet with the most common and useful Git commands.
Branches
See all local branches:
$ git branch
See all remote branches:
$ git branch -r
Create feature
branch from main
branch:
$ git checkout -b feature main
Create feature
branch from current branch:
$ git checkout -b feature
Delete a local branch local-branch
(might be refused if it contains unmerged/unpushed commits):
$ git branch -d local-branch
Delete a local branch local-branch
completely, even if it contains unmerged/unpushed commits:
$ git branch -D local-branch
Delete a remote branch remote-branch
:
$ git push origin --delete remote-branch
Remotes
Show remote URL of current repository:
$ git config --get remote.origin.url
Switching remote URLs from HTTPS to SSH
$ git remote set-url origin git@gitlab.com:username/repository.git
Remove
Stage a file for removal, but do not remove it from the working dir. The file will then be untracked.
$ git rm -r --cached
Reset
With git reset
[2] you can reset the current HEAD to a specified state.
Hard
git reset --hard HEAD
- Set Git back to HEAD
git reset --hard HEAD^
- Set Git back to the commit before the HEAD
git reset --hard HEAD~n
- Set Git back to the n-th commit before the HEAD
Git Large File Storage (LFS)
Install Git-LFS:
$ git lfs install
Git-LFS help pages can be found running any of these two commands:
$ git lfs help
$ git lfs -h
Manage file types to be handled Git LFS, you can edit .gitattributes
directly or add, e.g. .png
file types:
$ git lfs track "*.png"
or remove file types:
$ git lfs untrack "*.png"