Skip to content

Instantly share code, notes, and snippets.

@paesku
Last active February 1, 2020 15:02
Show Gist options
  • Save paesku/cef4ea174582b6aa667e to your computer and use it in GitHub Desktop.
Save paesku/cef4ea174582b6aa667e to your computer and use it in GitHub Desktop.
Helpfull git commands condensed into one file.

Git cheat sheet

There is a wunderfull Book about Git available in German: http://gitbu.ch/git.pdf
And a quickstart Site http://rogerdudler.github.io/git-guide/ or https://ohshitgit.com/ if it's to late.

Contents

  1. Remotes
  2. Branches
  3. Working Area
  4. Index
  5. Committing
  6. Tagging
  7. Files
  8. Stash
  9. Merging
  10. Informations
  11. Tracking
  12. Gitconfig

These commands assume following concept for branching:

feature-branches

List remotes

git remote -v

Add a remote

git remote add another-origin <git-repo-url>

Remove a remote

git remote rm <remote-name>

List all branches

git branch -a

List remote branches

git branch -r

Basics

Create Clean Branch

• create and checkout branch
• origin/ is starting point
• and --no-track avoids tracking that starting point as upstream (which it is not)

with the last two dashes you will receive a clear error output in case you made a mistake, it ensures that everthing before are no paths

git checkout -b <feature-branche> --no-track origin/<main-branch> --

Delete Branch

To delete a local branch

git branch -d <the-local_branch>

Delete all merged local branches. Be aware!

git branch --merged | grep -v \* | xargs git branch -D 

To delete a remote branch (push is the only command which gives you remote access)

git push origin :<the-remote-branch>

Rename Branch

Rename Branch locally

git branch -m <old-name> <new-name>

Push new Branch to remote

git push origin <new-name>:master

Clean Branch without history (Orphan Branch)

git checkout -o <branch-name>    

clear staging area

git reset

Remove from staging

git reset HEAD -- .

show local commit not pushed yet

git log @{u}..

clean working directory

git reset --hard

git clean -f -d

remove file from commit

git reset --soft HEAD~1

git reset HEAD <path-to-file>

Remove file before commit

git checkout <path-to-file>

Add file to Index

git add <path-to-file> or <path-to-folder>

Add all files to Index

git add .

Add changed files to Index

git add -u

Remove file from Index

git rm --cached <path-to-file>

Commit files with a one-liner

git commit -m "<commit message>"

Commit files with preset Editor

git commit

Empty Commit

git commit --allow-empty -m "<commit message>"

Undo last commit and keep changes

git reset HEAD^

Add a tag

git tag --annotate -m "<commit message>" <tag-name>

Push added tag

git push origin <tag-name>

Find latest commit where file existed

git rev-list -n 1 HEAD --  <path-to-file>

Re add deleted file

git checkout <deleting_commit>^ -- <path-to-file>

Save custom named stash

git stash save "stashname" 

Show all stashes

git stash list

Show Stash content

git stash show -p stash@{n}

To apply a stash and remove it from the stash list, run:

git stash pop stash@{n}

To apply a stash and keep it in the stash cache, run:

git stash apply stash@{n}

To remove a stash

git stash drop stash@{n}

Basics

Update Branches

git fetch

git merge --no-ff <main-branch>

Sync Branches

pull changes from remote, rebase local commits to the remote-head, do not try to fix conflicts

git pull --ff-only --rebase

Handle conflicts the hard way

Keep mine

git checkout --ours path/to/file

Keep other

git checkout --theirs path/to/file

Reverse Merge

update local snapshot of remote repository update your local branch to include commits from other developers on that branch

git fetch --prune
git checkout <feature-branch>
git merge --ff-only

if that branch is already checked out

git pull --ff-only --prune

keep the automatic commit message

git merge origin/<main-branch>

in case of conflicts

git status 
edit <file>
git add/rm <file>
git commit

publish the commit merge

git push

Check which branches are merged into another

git branch --merged <branch-to-check-against>

Cherry Picking

git cherry-pick <commit-hash>

Edit commit message

git cherry-pick -e <commit-hash>

Get information about changed files between to commits.

git diff [SHA1]..[SHA2] --name-only

Show all commits

git reflog

Show Branches which contain a specific commit. Usefull to search for merges.

git branch -a --contains <hash oder branch-name>

Show Informations about a particular file

git log --follow filename

Find commit where file was added

git log --diff-filter=A -- filename

or to get the commit message

git log --oneline filename | tail -1

find all TODO's

git grep -E "# TODO|// TODO"

Example config

Back to top

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment