I had to go back six commits in Git with the command
git checkout idofthecommit
What I have been doing was continuing commit, commit, commit. Since I have created no real new branch, when I git push to external repositories with
git push origin master
it tells me that everything is up-to-date.
This means I haven’t actually made any changes in the master.
How I can move the commits I have been doing to the master branch?
If your description of the commands you ran is correct – create a branch now on what you’ve been working on to create a reliable named reference to it (
git branch WIP HEAD). Then rebase against master (git rebase master) and checkout master and merge in WIP.You can test this out fairly simply to replicate the situation and explore the commands you need and what they do. For this
git graphis aliased togit log --graph --oneline --decorate --abbrev-commitand just produces a pretty git log.Create a demo repository with 8 commits then checkout six back and create some more commits
So lets just look at this:
So really this just looks like a feature branch taken from 8d5a6e1. The only special thing is its only reference is HEAD. If you now add a branch reference it will become a normal feature branch.
If you accidentally checkout master or some other branch before you labelled this working branch with a branch tag then you would loose the reference to the top of this new chain of commits. Now you lost the branch. In this case you need to use the reflog to find the last commit on that chain and add a branch reference to that commit. We can see this from the demo repository above:
We can see from the reflog that a few commits back, we checked out HEAD~6 and then added three more commits. We could then issue
git branch recover-commits e81b31cto get a branch that points to that set of commits and recover them.