Most common git screwups/questions and solutions

I was looking to learn a bit more about the parts of git I’ve not ventured into yet. What better way that looking the most common ways people screw them up and how to fix the resulting problems! Here’s a short list, compiled from my own experience and issues I’ve come across on the Internet.

I wrote the wrong thing in a commit message

If the commit hasn’t been push you can do the following, which will allow you to edit the message on the most recent commit:

git commit --amend

How can I undo the last commit?

You can use git reset e.g.:

git reset --hard HEAD~1

HEAD~1 means HEAD-1 commit. It should be noted that this is the nuclear option, and any changes you made will be discarded. If you want to keep your changes in the working tree use:

git reset --soft HEAD~1

If you’ve already published your commits, you should use revert. This is create new commits undoing the last change:

git revert HEAD~1..HEAD
git revert commitid

Delete a Git branch remotely

git push origin --delete branchname

What are the differences between ‘git pull’ and ‘git fetch’?

git pull, is git fetch followed by git merge. git fetch gets the remote changes, they get kept under refs/remotes//. However it doesn’t affect your local branches, and won’t change your working copy. git pull then merges these changes with the local copy.

How do I undo a ‘git add’ before committing

You did a “git add filename” accidentally and want to undo it before committing your change. You can simply do:

git reset filename

To unstage your changes to that file.

How do I deal with merge conflicts

Use “git mergetool” which gives a handy interface for solving merge conflicts.

Remove all local untracked files (and directories) from your local clone

Careful! You might want to take a backup before doing this:

git clean -f -d

Clone all remote branches

You probably already have, they’re just hiding! Use the following to see all the branches:

git branch -a

You can then use “git checkout origin/branchname” to take a look at the branch. Or “git checkout -b branchname origin/branchname”. To create a local tracking branch.

Rename local branch?

git branch -m oldname newname

Revert to a previous Git commit

You can use reset as above to revert to a previous commit, this assumes you mean go back to where you were before permanently rather than just take a look (for that you want to checkout an old version). The commit ID, should be as shown in the output of “git log”.

git reset --hard commitid

Again this will discard all changes in your working tree, so make sure this is really what you want to do! Or look at using –soft rather than –hard.

Remove a git submodule

Creating a submodule is pretty straight-forward, but deleting them less so the commands you need are:

git submodule deinit submodulename   
git rm submodulename
git rm --cached submodulename
rm -rf .git/modules/submodulename

Over-write local files when doing a git pull

Git reset is your friend again:

git fetch --all
git reset --hard origin/master

How can I add an empty directory to my repository?

You can’t! git doesn’t support this, but there’s a hack. You can create a .gitignore file in the directory with the following contents:

# Ignore everything in this directory
*
# Except this file
!.gitignore

I don’t believe it actually matters what’s in the .gitignore (and this .gitignore will ignore all files).

git export, exporting the source code as with “svn export”

Use git archive e.g:

git archive --format zip --output /full/path/to/zipfile.zip master 

Discard all my unchecked in changes

git checkout -- .

Create a new remote branch from the current local one

git config --global push.default current
git push -u

Restore a deleted file

First find the commit when the file last existed:

git rev-list -n 1 HEAD -- filename

Then checkout that file

git checkout deletingcommitid^ -- filename

Revert a single file to a specific previous commit

Similar to the above, but a bit simpler:

git checkout commitid filename

Make git ignore permissions/filemode changes

git config core.fileMode false

Get git to remember my password when checking out with https

It’ll only remember your password for 15mins:

git config --global credential.helper cache

You can make it remember your password longer with:

git config --global credential.helper "cache --timeout=XXXX"

Take a quick look at an old revision of a single file

git show commitid:filename

27 Comments

  1. Thanks for the nice summery of usefull Git info.

  2. Jonathan A Dunlap says:

    Great write up, thanks!

  3. Jeff says:

    I would add one: if you want to undo the last rebase you completed: git reset –soft ORIG_HEAD

    (or –hard if you’re confident/adventurous)

  4. Rich Wareham says:

    A very useful article which will live in my bookmarks, but…

    > However it doesn’t effect your local branches, and wont change your working copy.

    ITYM “affect” your local branches and “won’t” change your working copy. These two common English errors nestling together in the same sentence just caught my eye.

  5. Sean Wallitsch says:

    Quick note:

    “git push origin –delete branchname”

    Only deletes the branch off the remote, not both locally and remote- unless this has changed over git versions?

  6. hasenj says:

    For remembering passwords I would use ssh keys ..

  7. Jason Noble says:

    It’d be cool if you could add some name attributes to your H2’s. That way we could link to http://41j.com/blog/2015/02/common-git-screwupsquestions-solutions/#discard_unsaved_changes.

  8. hasenj says:

    Also, keep in mind there’s actually a `git revert` command that lets you undo changes without altering previous history.

    `git reset –hard` is far more likely to introduce screw ups than solve them (at least in the hands of a beginner). `git revert` is far safer.

  9. Fred says:

    Basically summed up a typical work day for me… I screw up, a lot. Cheers!

  10. new299 says:

    Thanks for this! I’ve updated the page, it might take a while for the cache to update.

  11. new299 says:

    Yes ssh is the way to go if at all possible. There are a couple of situations I’ve had in the past when ssh wasn’t available for various reasons (though I didn’t know about caching at that point). It is also a frequently asked question (if it’s really a good idea is another question of course :))

  12. exscape says:

    There’s a typo (I assume) in the git pull section. It says that pull is fetch followed by merge, and then says fetch gets the remote changes, and “git pull then merges these changes” — I assume it should be “git merge then merges these changes with the local copy”, though without context both are sort of true.

  13. Snild Dolkow says:

    “I lost my commit somehow (or made a bad amend)”:

    git reflog
    [then use one of the commands above that take a commitid]

  14. Andrea Zonca says:

    It would be useful to specify that the credential cache timeout is in seconds.

  15. Thayne McCombs says:

    There is a convention of putting a file called .gitkeep in empty directives to add them to git.

  16. You should make a note of the operations which have potentially terrifying side effects if you’re not working completely on your own.

  17. julien says:

    For the empty directory you can also add a .gitkeep file in that directory, it’s “almost” the same

  18. Joop Kiefte says:

    Actually, instead of a .gitignore file, it can be any throwaway-file (SparkleShare uses a hidden file .empty with the contents This is a folder! iirc)

  19. new299 says:

    Thanks for pointing this out! I’ve updated the post though it might take a while to update the cache.

  20. new299 says:

    Yes, your absolutely right, and this is an all round better idea. However there are occasional situations when you’re forced to run over https, and (though it may not be a great idea), how to cache passwords is a commonly asked question.

  21. jhallard says:

    Thank you for this!

  22. Guilherme Moura Nascimento says:

    Nice!

  23. Matthieu Moy says:

    git reset –hard is almost never the right idea. If you want to undo the last commit, then “git reset –keep” does the right thing: discard the last commit, but keep your uncommited changes. If you want to discard your local changes, then “git stash” does in, but in a safe way (there’s a git stash pop, but nothing to undo a “reset –hard”).

  24. Luis Lasser says:

    That line of code was a lifesaver for me. In the company I work on, they had several ports blocked by firewall, so it’s imposible to use ssh

Leave a Reply