Fixing a messed up git directory

Following situation:
You have got a cloned repository in your home directory, you noticed that something changed upstream und you want to get all the fancy changes. You do a:

git fetch upstream && git pull upstream tags/2.1.0

to get the latest tag. What you didn’t check: What is your current branch, have you uncommited changes? And boom, everything is broken:

$ git status
Auf Branch master
Ihr Branch ist auf dem selben Stand wie 'origin/master'.
Sie haben nicht zusammengeführte Pfade.
 (beheben Sie die Konflikte und führen Sie "git commit" aus)

zum Commit vorgemerkte Änderungen:

	neue Datei:     .coveralls.yml
	neue Datei:     .fixtures.yml
	neue Datei:     .gitignore
	neue Datei:     .gitmodules
	neue Datei:     .sync.yml
	neue Datei:     .travis.sh
	neue Datei:     .travis.yml

Nicht zusammengeführte Pfade:
  (benutzen Sie "git add/rm <Datei>...", um die Auflösung zu markieren)

	von beiden hinzugefügt: CHANGELOG.md
	von beiden hinzugefügt: Gemfile
	von beiden hinzugefügt: README.md
	von beiden hinzugefügt: lib/puppet/provider/kernel_parameter/grub2.rb
	von beiden hinzugefügt: metadata.json
	von beiden hinzugefügt: spec/spec_helper.rb

How do we fix that?
We start with:

git reset --hard

What does it do?

Resets the index and working tree. Any changes to tracked files in the working tree since
are discarded.

Than we add:

git clean --force -d

This basically deletes all untracked directories and files. We can simulate this with:

git clean --force -d --dry-run

Now we got a clean directory again, all files with merge conflicts or whatsoever are gone. Please note that this will also remove every change that you did before the pull but forgot to commit. If you want to keep that changes than you shouldn’t copy&paste the git commands!

This entry was posted in 30in30, General, Linux, Short Tips. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.