My Git cheat sheet

Init an empty Git repository or reinitialize an existing one

git init

Clone a repository

git clone https://mygitbucket.com/niklas/my_project.git

Pull in new changes from remote

git pull

Do a dry-run fetch to see what is comming

 git fetch --dry-run

Create a branch locally and checkout the branch

git checkout -b <name of branch>

Push the branch to remote

 git push origin <name of branch>

Delete local branch

git branch -d <name of branch>

Delete remote and local

git branch -D <name of branch>

Show all branches

 git branch -a

Add a remote subtree to your project. ‘common’ is here the name of the subtree

git remote add common https://mygitbucket.com/niklas/my_project.git

Do a ‘common pull’ i.e. pull a remote branch into to subtree. ‘common’ is here the name of the subtree (and folder)

git subtree pull -P common common my-subtree-branch --squash

List all remotes

git remote -v

Rename a local branch that you have checked out

git branch -m <new name>

Rename a branch you have not checked out

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

Check commits only in the current branch (descendent to master)

git log master..

Check commits only in the current branch (descendent to other branch)

git log master..<name of branch>

Create patch files for every commit in a interval of SHA’s

git format-patch <start SHA>..<stop SHA>

HEAD and other short codes work too
Create ONE patch file for all commits in the last 10 commits (from HEAD and back)

git format-patch -10 HEAD --stdout > my.patch

Apply a patch

git apply my.patch

Stash all uncommited modifications

git stash

Apply last saved stash to your current code

git stash apply

Apply last saved stash to your current code AND delete the stash

git stash pop

Manually delete the stash

git stash drop

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">

This site uses Akismet to reduce spam. Learn how your comment data is processed.