Useful Git Commands

Aug 4, 2020

Here I collect useful git commands that I use very often. I don’t go into details. It’s just a convenient list to look up commands.

Commits

Add changes to the previous commit:

git commit --amend

Undo the last commit, but keep changes:

git reset --soft HEAD~1

To avoid that changes of specific files get committed (e.g. for local configurations, etc.):

git update-index --assume-unchanged .config/params.cnf

Branches

Checkout single file from another branch:

git checkout otherBranch -- file.kt

Diff of a file on other branch:

git diff release/1.0.0 release/2.0.0 -- app/build.gradle

Rename branch (locally):

git branch -m new-name

Delete remote branch:

git push origin --delete $BARNCH_NAME

Overwrite local branch with remote changes (a.k.a. “force pull”):

git fetch --all
git reset --hard origin/$BARNCH_NAME

Tags

Create a tag:

git tag $TAG_NAME

List tags:

git tag --list

Push tag:

git push origin $TAG_NAME

Remove local tag:

git tag -d $TAG_NAME

Remove remote tag:

git push origin :refs/tags/$TAG_NAME

Untracked files

Show untracked files (that will be deleted by git clean):

git clean -n

Delete untracked files and folders:

git clean -fdX

Remove empty directories:

find . -type d -print | xargs rmdir 2>/dev/null

Restore

Restore a deleted file, but the deletion is not commited yet:

git checkout HEAD $FILENAME

Credentials

Store git credentials locally, so you don’t have to enter them again (Note: They’re stored unencrypted!):

git config --global credential.helper store

You might also like