Git came back 5 commits and forgot to create a new branch, how to get back?

I had to revert six commits to Git using the command

git checkout idofthecommit

      

What I was doing was keep committing, committing, committing. Since I haven't created any real new branch when I Git push to external repos with

git push origin master

      

he tells me everything is up to date.

This means that I have not actually made any changes to the master.

How can I move the commits I was making with the master branch?

+3


source to share


1 answer


If your description of the commands you run is correct - create a branch now on what you were working on to create a reliable named link to it ( git branch WIP HEAD

). Then reinstall the wizard ( git rebase master

) and verification wizard and merge into WIP.

You can test this out simply enough to reproduce the situation and learn the commands you need and what they do. It git graph

has an alias for this git log --graph --oneline --decorate --abbrev-commit

and just creates a pretty git log.

Create a demo repository with 8 commits, then go back six and create a few more commits

$ cd /tmp && git init demo && cd demo
$ for msg in one two three four five six seven eight; do echo $msg>file && git add file && git commit -m $msg; done
$ git checkout HEAD~6
$ for msg in nine ten eleven; do echo $msg>file && git add file && git commit -m $msg; done

      

So let's just take a look at this:



$ git graph --all
* e81b31c (HEAD) eleven
* c005e75 ten
* c567d25 nine
| * 4d28c3d (master) eight
| * 380f715 seven
| * 9966c80 six
| * 6b2f757 five
| * e43d079 four
| * ce0ff34 three
|/
* 8d5a6e1 two
* 1f880ae one

      

So this really looks like a feature branch taken from 8d5a6e1. The only feature is its only mention - HEAD. If you now add a link to a branch, it becomes a normal feature branch.

If you accidentally checked out master or some other branch before you tagged that working branch with a branch tag, you will lose the link to the top of this new commit chain. Now you have lost your branch. In this case, you need to use reflog to find the last commit in this chain and add a link to that commit. We can see this from the demo repository above:

$ git reflog
e81b31c HEAD@{0}: commit: eleven
c005e75 HEAD@{1}: commit: ten
c567d25 HEAD@{2}: commit: nine
8d5a6e1 HEAD@{3}: checkout: moving from master to HEAD~6
4d28c3d HEAD@{4}: reset: moving to 4d28c3d
....

      

From the reflog you can see that some of them returned, we checked HEAD ~ 6 and then added three more commits. We could then checkout git branch recover-commits e81b31c

to get the branch that points to that set of commits and restore them.

+6


source







All Articles