/ dsp2017

My git is broken, help!

OK, so you've managed to 'break' your repository. Some files that should not be tracked are tracked. History is not at it was supposed to be. You can't push nor pull.

Basically this:

Michael Bay's repository?

Clean status

Do you have a clean repository on git status?

Creidhne:EnviroMonitorWeb hasik$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)
nothing to commit, working tree clean

All clean? No files untracked, modified, deleted, added?
Good, skip this section below.

Nope?

Did you...

...staged something you don't want?

git reset HEAD file_to_unstage

...modified something you don't want modified?

git checkout -- file_to_unmodify

...get stuck in the middle of a rebase?

git rebase --abort

...get stuck in the middle of a rebase and can't abort this rebase?

git update-ref refs/heads/master last_head_commit_sha1

No untracked files

Run:

git clean -n

Are there any files listed? These are untracked so unless you are ready to commit them move them somewhere else (or git add them and run git stash). If you do not feel emotionally attached to any of those files nuke them: git clean -fd.

OK, now we can do something about the root cause of your problem.

Run git pull --rebase. If you messed with your history then you probably can't push your changes back. If you are the only one using this branch and/or repository you can push with force. Only then. No exceptions.

Still not working

Git stores a list of operations. Provided your repository was clean and working at one point you can try going back in time.

Not like that

Run:

git reflog

You'll get something like:

Creidhne:EnviroMonitorWeb hasik$ git reflog
568c2c2 HEAD@{0}: commit (amend): Extract station token match method to model
1fa00fd HEAD@{1}: commit: Extract method to model
dcec293 HEAD@{2}: commit: Remove unused variable
6aaa4ee HEAD@{3}: reset: moving to HEAD
6aaa4ee HEAD@{4}: reset: moving to HEAD
6aaa4ee HEAD@{5}: pull --rebase: Fast-forward
e30ca0b HEAD@{6}: checkout: moving from use-uuids-for-model-ids to master
16b9283 HEAD@{7}: commit (amend): Fix typos in README.md

Assume I want to undo the amended commit.

I would run

git reset --hard HEAD@{1}

And just like that, I'm back in time.