git

Diff

Diff between branches

git diff branch1..branch2

which is equivalent to

git diff branch1 branch2

Diff of one file path

git diff branch1..branch2 -- myfile.cs

Diff of a directory between branches

git diff master..mybranch path/to/dir

Diff of one file between version in given commit and local HEAD

git diff 2f43f286 index.php

Show the number of lines changed

git diff --stat
git diff --numstat

Diff from a common ancestor

git diff [options] commitA...commitB [--] [path...]

This form is to view the changes on the branch containing and up to commitB, starting at a common ancestor of both commit.

git diff A...B

is equivalent to

git diff $(git merge-base A B) B

Use separate tool (e.g. Meld) to view diff

git difftool --dir-diff branch1 branch2

Rebase

Rebase onto

git rebase --onto newbase from-commit-after branch-to-move

Branches

Create a branch and switch to it at the same time

git checkout -b iss53

Delete a remote branch

git push origin --delete feature/login

Delete a local branch

git branch -d branch-name

Delete a remote tracking branch

git branch -d -r remote-name/branch-name

Rename a branch

git branch -m <old> <new>

Remotes

Initialize a repo with no working directory e.g. a shared repository. Developers can clone our-shared-project.git.

git init --bare <our-shared-project.git>

Change url of remote

git remote set-url origin new-url

Fetch one branch from a remote

git fetch <remote_name> <branch_name>

Rename remote

git remote rename from to

Remove remote

git remote remove temp-remote

Tags

Show tags

git tag

Add a tag

git tag tagname

Push tag to remote

git push origin tagname

Logging

git log --pretty=oneline
git log --graph --oneline --decorate --all

Misc.

Stop tracking a folder

git rm -r --cached path_to_dir/