In a bad state in git

After some rebase, merge and git stash, sometimes I found myself in a bad git state, like a detached head state or in the middle of some messy conflict resolving, whatever that means. What to do?

First question to ask yourself, can I afford to throw away my current changes?


If the answer is yes, then there are many options. Here are a few:

rm -rf git_dir/
git clone https://some.git.clone.path

This is probably the most noob way to back out from a bad state.

git reset --hard HEAD

With git reset, you go back in history to the beginning of your current branch.


If the answer is no, then here are a few options:

Use whatever cmds to tar/gzip your git directory. 

Then assuming you are on your current branch called bad_branch, try

git checkout master
git checkout -b new_branch
git checkout bad_branch -- .

The last command will copy the changed files (instead of commits) over to your new_branch. Then you can do your clean up there. Your current new_branch will be one commit away from master (change this accordingly).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s