Git Cheatsheet

Git commands for often used tasks.

Branches

Create remote branch

git checkout -b your_branch
git push -u origin your_branch

Create branch from uncommited code

git checkout -b new_branch
git add .

Delete branch (remote and local)

git push origin :branch_name  # remote
git branch -d branch_name     # local

Get remote branch

git checkout -b branch_name
git pull origin branch_name

Undoing

Roll back to revision

git reset --hard <tag/branch/commit id>

Roll back specific file to last commit

git checkout -- filename

Discard current changes to last commit

git checkout -- .

Show repo URL

git remote show origin

Edit last commit message

git commit --amend

Show diff for last commit

git diff HEAD^ HEAD

Reattach detached HEAD

git merge HEAD@{1}

Completely remove file from repository history

git filter-branch --prune-empty --index-filter "git rm --cached -f --ignore-unmatch <path/to/file/here>" --tag-name-filter cat -- --all

Then do a clone using local filesystem:

git clone path/to/old/local/repo.git repo