Saving your back in git

Here is a usefull collection of git commands that I keep coming back to, it saved me countless times when I was in tricky situations. I have finally decided to compile them here, I hope it can help you too!

Table of Contents

Rename a local branch

Rename a branch while pointed to any branch:

git branch -m oldname newname

Rename the current branch:

git branch -m newname

Delete a remote tag

Remove a tag on the origin:

git push -d origin tagname

Remove a tag locally:

git tag -d tagname

Check out a remote branch

You can be in a situation when you need to check out a remote branch locally. This branch has been pushed to the remote repository, by someone else or you.

git pull --all
Fetching origin
remote: Counting objects: 1, done.
remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 1
Unpacking objects: 100% (1/1), done.
From github.com:github-user/my-repo
XXXXf37..XXXXb28  my-branch     -> origin/my-branch
...

You can then checkout out this branch:

git co my-branch

List all the files in a commit

git diff-tree --no-commit-id --name-only -r my_commit_id
test.txt
src/lib.rb

Information about the arguments:

  • The --name-only show only names of changed files.
  • The --no-commit-id removes the commit ID output.
  • The -r argument is to recurse into sub-trees.

For listing files from the parent commit, use a -m argument:

git diff-tree --no-commit-id --name-only -m -r my_parent_commit_id

Remove files from a commit

You have mistakely commited files locally, and you want this files to be back in the staging area without undoing the work that has been done on them.

git reset --soft HEAD~1

git reset HEAD path/to/file_to_remove

git commit -c ORIG_HEAD

Alternatively, you can define this with a git alias:

git config --global alias.undo 'reset --soft HEAD~1'

Undo a git commit –amend

# Move the current head so that it's pointing at the old commit
# Leave the index intact for redoing the commit.
# HEAD@{1} gives you "the commit that HEAD pointed at before
# it was moved to where it currently points at". Note that this is
# different from HEAD~1, which gives you "the commit that is the
# parent node of the commit that HEAD is currently pointing to."
git reset --soft HEAD@{1}
# commit the current tree using the commit details of the previous
# HEAD commit. (Note that HEAD@{1} is pointing somewhere different from the
# previous command. It's now pointing at the erroneously amended commit.)
git commit -C HEAD@{1}

Thanks to this Stackoverflow question.

Ignore files already commited

Create a new branch to apply the effective removal of files.

git checkout -b new-branch

Apply the removal of the file, without removing the local copy.

git rm --cached my-file

It’s now safe to commit the file and push it to the remote.

git add . && git commit my-file

Exclude a file from a diff

git diff -- . ":(exclude)path/to/file-to-exclude.rb"

Exclude files from the diff based on the extension:

git diff -- . ":(exclude)*.min.js" ":(exclude)*.min.css"

Usefull resources