git rebase

Updated: 24 May 2023

Rebase onto

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

If required to squash into the first commit of a repository

git rebase --interactive --root

git ignore

Updated: 25 January 2024

Git ignore uses globbing patterns.

Ignore .env file in current directory only and not .env files which might occur further down the directory structure

# .gitignore

/.env

Find which .gitignore entry is responsible for ignoring a particular file

git check-ignore --verbose file/to/check.cs

Show all files being ignored, recursively

find . -type f  | git check-ignore --verbose --stdin

git log

Updated: 26 March 2024

All the refs in refs/

git log --all

All the refs in refs/heads

git log --branches

All the refs in refs/remotes

git log --remotes

All the refs in refs/tags

git log --no-walk --tags

Draw a text-based graphical representation of the commit history

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

Refresh git log periodically (every second) with watch utility

watch --color -n 1 git log --graph --oneline --decorate --all --color=always

Show commit which added a file

git log --diff-filter=A -- dotnet.code-workspace

Shows commits that changed a single file, including those commits that occurred before the file was given its present name.

git log --follow builtin/rev-list.c

Find commits by a particular author

git log --author='Chris Taylor'

git remote

Updated: 29 March 2024

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 origin origin_bitbucket

Remove remote

git remote remove temp-remote

Add a remote

git remote add origin git@github.com:User/UserRepo.git

Ping a remote by listing it’s references

git ls-remote

git tags

Updated: 20 January 2024

Show tags

git tag

Add a tag to current commit

git tag tagname

Add an annotated tag to a specific commit

git tag -a tag_name commit_id -m "message"

Push tag to remote

git push origin tagname

List tags with date (from Stackoverflow)

git for-each-ref --sort=creatordate --format '%(refname) %(creatordate)' refs/tags

Delete a tag

git tag -d foo-tag

git branch

Updated: 18 March 2024

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 remote tracking branch

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

Remove upstream info for the_branch.

git branch the_branch --unset-upstream

Delete a local branch

git branch -d branch-name

Rename a branch

git branch -m <old> <new>

List all branches

git branch -a

Move branch pointer to another arbitrary commit

git branch --force <branch> <commit>

Create new branch at arbitrary commit

git branch branch_name commit_hash

Push all branches to a remote

git push origin --all

git diff

Updated: 31 December 2023

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

Diff two directories not tracked by Git

git diff --no-index Downloads Downloads-copy

Highlight changed words using only colors

git diff --color-words

Generate diffs with n lines of context instead of the usual three

git diff --unified=0
git diff -U0

Show only names of changed files

git diff --name-only master media-coverage

git

Updated: 31 March 2024

Use a bare git repo to control a working-tree in another directory

git --work-tree=path/to/my-proj --git-dir=path/to/bare-repo.git <usual git commands>

Stop tracking a folder

git rm -r --cached path_to_dir/

In a tracked directory show the location of the .git folder

git rev-parse --show-toplevel

Auto commit and push

for run in {1..1000}; do git add . && git commit -m 'auto save'; git push bb-chris; sleep 45; done

List all files currently tracked in that_branch

git ls-tree -r that_branch --name-only