git-cheat-sheet

git-cheat-sheet

Git cheatsheet

Git-Cheat-Sheet

A cheat sheet for uncommon Git commands

Configuration

Command Description
git config --global user.name "foo" Set user name
git config --global user.email "foo@example.com" Set user email

Branches

Command Description
git branch foo Create a new branch
git branch -d foo Deletes a branch
git switch foo Switch to a branch
git switch -c|--create foo Create and switch to a branch
git checkout foo.js Undo all changes on the foo.js file
git checkout foo Use git switch instead
git checkout -b foo Use git switch -c instead
git merge foo Merge branch into current branch

Pulling

Command Description
git pull --rebase --prune Get latest, rebase any changes not checked in and delete branches that no longer exist

Staged Changes

Command Description
git add file.txt Stage file
git add --patch file.txt Stage some but not all changes in a file
git mv file1.txt file2.txt Move/rename file
git rm --cached file.txt Unstage file
git rm --force file.txt Unstage and delete file
git reset HEAD Unstage changes
git reset --hard HEAD Unstage and delete changes
git clean -f|--force -d Recursively remove untracked files from the working tree
git clean -f|--force -d -x Recursively remove untracked and ignored files from the working tree

Changing Commits

Command Description
git reset 5720fdf Reset current branch but not working area to commit
git reset HEAD~1 Reset the current branch but not working area to the previous commit
git reset --hard 5720fdf Reset current branch and working area to commit
git commit --amend -m "New message" Change the last commit message
git commit --fixup 5720fdf -m "New message" Merge into the specified commit
git revert 5720fdf Revert a commit (use git log to check commit ids)
git rebase --interactive [origin/main] Rebase a PR (git pull first)
git rebase --interactive 5720fdf Rebase to a particular commit
git rebase --interactive --root 5720fdf Rebase to the root commit
git rebase --continue Continue an interactive rebase
git rebase --abort Cancel an interactive rebase
git cherry-pick 5720fdf Copy the commit to the current branch

Compare

Command Description
git diff See difference between working area and current branch
git diff HEAD HEAD~2 See difference between te current commit and two previous commits
git diff main other See difference between two branches

View

Command Description
git log See commit list
git log --patch See commit list and line changes
git log --decorate --graph --oneline See commit visualization
git log --grep foo See commits with foo in the message
git show HEAD Show the current commit
git show HEAD^ or git show HEAD~1 Show the previous commit
git show HEAD^^ or git show HEAD~2 Show the commit going back two commits
git show main Show the last commit in a branch
git show 5720fdf Show named commit
git blame file.txt See who changed each line and when

Stash

Command Description
git stash push -m "Message" Stash staged files
git stash --include-untracked Stash working area and staged files
git stash list List stashes
git stash apply Moved last stash to working area
git stash apply 0 Moved named stash to working area
git stash clear Clear the stash

Tags

Command Description
git tag List all tags
git tag -a|--annotate 0.0.1 -m|--message "Message" Create a tag
git tag -d|--delete 0.0.1 Delete a tag
git push --tags Push tags to remote repository

Remote

Command Description
git remote -v List remote repositories
git remote show origin Show remote repository details
git remote add upstream <url> Add remote upstream repository
git fetch upstream Fetch all remote branches
git rebase upstream/main Refresh main branch from upstream
git remote -v List remote repositories
git push --tags Push tags to remote repository

Submodules

Command Description
git submodule status Check status of all submodules
  • Pull submodules

    1. git submodule sync
    2. git submodule init
    3. git submodule update
  • Change branch

    1. cd /submodule
    2. git fetch origin <branch-name>
    3. git checkout <branch-name>
    4. cd /

    Git

Global Settings

Reminder

Press minus + shift + s and return to chop/fold long lines!

Show folder content: ls -la

Notes

Do not put (external) dependencies in version control!

Setup

See where Git is located:
which git

Get the version of Git:
git --version

Create an alias (shortcut) for git status:
git config --global alias.st status

Help:
git help

General

Initialize Git:
git init

Get everything ready to commit:
git add .

Get custom file ready to commit:
git add index.html

Commit changes:
git commit -m "Message"

Commit changes with title and description:
git commit -m "Title" -m "Description..."

Add and commit in one step:
git commit -am "Message"

Remove files from Git:
git rm index.html

Update all changes:
git add -u

Remove file but do not track anymore:
git rm --cached index.html

Move or rename files:
git mv index.html dir/index_new.html

Undo modifications (restore files from latest commited version):
git checkout -- index.html

Restore file from a custom commit (in current branch):
git checkout 6eb715d -- index.html

Reset

Go back to commit:
git revert 073791e7dd71b90daa853b2c5acc2c925f02dbc6

Soft reset (move HEAD only; neither staging nor working dir is changed):
git reset --soft 073791e7dd71b90daa853b2c5acc2c925f02dbc6

Undo latest commit: git reset --soft HEAD~

Mixed reset (move HEAD and change staging to match repo; does not affect working dir):
git reset --mixed 073791e7dd71b90daa853b2c5acc2c925f02dbc6

Hard reset (move HEAD and change staging dir and working dir to match repo):
git reset --hard 073791e7dd71b90daa853b2c5acc2c925f02dbc6

Hard reset of a single file (@ is short for HEAD):
git checkout @ -- index.html

Update & Delete

Test-Delete untracked files:
git clean -n

Delete untracked files (not staging):
git clean -f

Unstage (undo adds):
git reset HEAD index.html

Update most recent commit (also update the commit message):
git commit --amend -m "New Message"

Branch

Show branches:
git branch

Create branch:
git branch branchname

Change to branch:
git checkout branchname

Create and change to new branch:
git checkout -b branchname

Rename branch:
git branch -m branchname new_branchname or:
git branch --move branchname new_branchname

Show all completely merged branches with current branch:
git branch --merged

Delete merged branch (only possible if not HEAD):
git branch -d branchname or:
git branch --delete branchname

Delete not merged branch:
git branch -D branch_to_delete

Merge

True merge (fast forward):
git merge branchname

Merge to master (only if fast forward):
git merge --ff-only branchname

Merge to master (force a new commit):
git merge --no-ff branchname

Stop merge (in case of conflicts):
git merge --abort

Stop merge (in case of conflicts):
git reset --merge // prior to v1.7.4

Undo local merge that hasn’t been pushed yet:
git reset --hard origin/master

Merge only one specific commit:
git cherry-pick 073791e7

Rebase:
git checkout branchname » git rebase master
or:
git merge master branchname
(The rebase moves all of the commits in master onto the tip of branchname.)

Cancel rebase:
git rebase --abort

Squash multiple commits into one:
git rebase -i HEAD~3 (source)

Squash-merge a feature branch (as one commit):
git merge --squash branchname (commit afterwards)

Stash

Put in stash:
git stash save "Message"

Show stash:
git stash list

Show stash stats:
git stash show stash@{0}

Show stash changes:
git stash show -p stash@{0}

Use custom stash item and drop it:
git stash pop stash@{0}

Use custom stash item and do not drop it:
git stash apply stash@{0}

Use custom stash item and index:
git stash apply --index

Create branch from stash:
git stash branch new_branch

Delete custom stash item:
git stash drop stash@{0}

Delete complete stash:
git stash clear

Gitignore & Gitkeep

About: https://help.github.com/articles/ignoring-files

Useful templates: https://github.com/github/gitignore

Add or edit gitignore:
nano .gitignore

Track empty dir:
touch dir/.gitkeep

Log

Show commits:
git log

Show oneline-summary of commits:
git log --oneline

Show oneline-summary of commits with full SHA-1:
git log --format=oneline

Show oneline-summary of the last three commits:
git log --oneline -3

Show only custom commits:
git log --author="Sven"
git log --grep="Message"
git log --until=2013-01-01
git log --since=2013-01-01

Show only custom data of commit:
git log --format=short
git log --format=full
git log --format=fuller
git log --format=email
git log --format=raw

Show changes:
git log -p

Show every commit since special commit for custom file only:
git log 6eb715d.. index.html

Show changes of every commit since special commit for custom file only:
git log -p 6eb715d.. index.html

Show stats and summary of commits:
git log --stat --summary

Show history of commits as graph:
git log --graph

Show history of commits as graph-summary:
git log --oneline --graph --all --decorate

Compare

Compare modified files:
git diff

Compare modified files and highlight changes only:
git diff --color-words index.html

Compare modified files within the staging area:
git diff --staged

Compare branches:
git diff master..branchname

Compare branches like above:
git diff --color-words master..branchname^

Compare commits:
git diff 6eb715d
git diff 6eb715d..HEAD
git diff 6eb715d..537a09f

Compare commits of file:
git diff 6eb715d index.html
git diff 6eb715d..537a09f index.html

Compare without caring about spaces:
git diff -b 6eb715d..HEAD or:
git diff --ignore-space-change 6eb715d..HEAD

Compare without caring about all spaces:
git diff -w 6eb715d..HEAD or:
git diff --ignore-all-space 6eb715d..HEAD

Useful comparings:
git diff --stat --summary 6eb715d..HEAD

Blame:
git blame -L10,+1 index.html

Releases & Version Tags

Show all released versions:
git tag

Show all released versions with comments:
git tag -l -n1

Create release version:
git tag v1.0.0

Create release version with comment:
git tag -a v1.0.0 -m 'Message'

Checkout a specific release version:
git checkout v1.0.0

Collaborate

Show remote:
git remote

Show remote details:
git remote -v

Add remote upstream from GitHub project:
git remote add upstream https://github.com/user/project.git

Add remote upstream from existing empty project on server:
git remote add upstream ssh://root@123.123.123.123/path/to/repository/.git

Fetch:
git fetch upstream

Fetch a custom branch:
git fetch upstream branchname:local_branchname

Merge fetched commits:
git merge upstream/master

Remove origin:
git remote rm origin

Show remote branches:
git branch -r

Show all branches (remote and local):
git branch -a

Create and checkout branch from a remote branch:
git checkout -b local_branchname upstream/remote_branchname

Compare:
git diff origin/master..master

Push (set default with -u):
git push -u origin master

Push:
git push origin master

Force-Push:
`git push origin master –force

Pull:
git pull

Pull specific branch:
git pull origin branchname

Fetch a pull request on GitHub by its ID and create a new branch:
git fetch upstream pull/ID/head:new-pr-branch

Clone to localhost:
git clone https://github.com/user/project.git or:
git clone ssh://user@domain.com/~/dir/.git

Clone to localhost folder:
git clone https://github.com/user/project.git ~/dir/folder

Clone specific branch to localhost:
git clone -b branchname https://github.com/user/project.git

Clone with token authentication (in CI environment):
git clone https://oauth2:<token>@gitlab.com/username/repo.git

Delete remote branch (push nothing):
git push origin :branchname or:
git push origin --delete branchname

Archive

Create a zip-archive: git archive --format zip --output filename.zip master

Export/write custom log to a file: git log --author=sven --all > log.txt

Troubleshooting

Ignore files that have already been committed to a Git repository: http://stackoverflow.com/a/1139797/1815847

Security

Hide Git on the web via .htaccess: RedirectMatch 404 /\.git
(more info here: http://stackoverflow.com/a/17916515/1815847)

Large File Storage

Website: https://git-lfs.github.com/

Install: brew install git-lfs

Track *.psd files: git lfs track "*.psd" (init, add, commit and push as written above)

Posted on

2020-12-26

Updated on

2021-01-31

Licensed under

Comments