Tag Archives: Git - Page 3

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

Exclude paths from the Git diff command

This is usefull if you for example have one or more subtrees in your project and you do not want to see changes made in those in your diff

Say you have a folder called ‘common’. This is how to exclude it from git diff:

git diff origin/master -- . ':!/common'

Now if you have a specific file that you want to exclude the command looks like this:

git diff origin/master -- . ':!/rules/form.xml'

And if you want to exclude multiple files of folders:

git diff origin/master -- . ':!/rules/form.xml' ':!/common'

NOTE! ‘origin/master’ is just an example and not part of the solution.

Tested on Git vesion 2.23.0 on Windows 10