ReinH Git Workflow
I am using this workflow:
where he mentions: “First, and while in your master branch (git checkout master), pull in the most recent changes:
git pull origin master
This should never create a merge commit because we are never working directly in master.”
Whoops, I just did. I accidentally committed a few changes to a couple of files to my master (vs my topic branch). I then realized my mistake – deleted my topic branch. Now I want to get rid of my committed changes (to the master), what is the right thing to do? I have not rebased yet or pushed my changes to repo, so it is all local.
In essence, I want to pull down the latest version of the master code and just disregard everything I have done to my local master (which is really one commit).
2 Solutions collect form web for “ReinH Git Workflow”
If you just want to throw it away, while you’re on your master branch:
git reset --hard HEAD^
This command will reset your master’s HEAD pointer to the previous commit. (You could also say “HEAD~1”, which means the same thing as HEAD^.) More generally, you can also reset your master to be the same as the server’s:
git reset --hard origin/master
This will work regardless of the state of master (i.e. 5 commits ahead of origin, or 30 commits behind it). The
--hard option in these commands means that the files in your working tree will also be reset along with the branch heads.
If you were in a similar situation where you actually wanted to keep your changes, you can always do:
git fetch # This grabs changes from the server without merging them git rebase origin
That will replay any work you have in master on top of the latest changes on the server.
Yet another alternative would be to create a topic branch for your work on master:
git checkout -b newfeature
Then you could switch back to master (git checkout master) and use the first command I gave to rewind master back one commit. Remember, branches are really just convenient names for commits in your repository. For example,
master is just a reference to the latest commit on that branch. You can move these references at will, and as you get more advanced with git, you’ll find yourself doing it fairly often.
I also recommend you get in the habit of running
gitk --all whenever you’re playing with branches, so you can visually see exactly what you’re doing. When I was new to git, I ran this command all the time. I still do…
All your commits lost by the branch deletion are there yet in git.
You can access all of them by traversing the reflog. Take a look to