My Git cheat sheet

INIT

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

BRANCH

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

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>

SUBTREE

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

LOG

Pretty print log with oneline

git log --pretty=oneline

Git log with graph

git log --all --decorate --oneline --graph

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

git log master..

Check commits that differ between branches (descendent to other branch)

git log master..<name of branch>

PATCH

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

Show the content of your current stash

git stash show

Show a diff of the content of your stash and current code base

git stash show -p

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

TAG

List all tags

git tag

Create a “lightweight” tag

git tag <tag name> <SHA*>

* SHA is optional. Without it you will tag the place you are at
Create a “heavyweight” tag

git tag -a <tag name> -m <message> <SHA*>

* SHA is optional. Without it you will tag the place you are at
Remove a tag

git tag -d <tag name>

MISC

List all commits waiting to get pushed

git cherry -v

Disable ssl verification on a specific domain

git config --global http."https://git.niklasottosson.com/".sslVerify false

Remove folder in Git and leave folder intact on disc

git rm -r --cached <folder>

-r is needed for folders (not files)
–cached is the magic the leaves the folder on disc and only removes it from Git

Set new http adress to origin

git remote set-url origin <new url eg. http://github.com/newrepo.git>

This changes both the (fetch) and (push) url for origin in your local repository

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.