I was doing some work on a git project, on the master branch, and made 2 commits. Then I thought I should really do this on a separate branch, instead of master. But I've already committed twice to master!
Luckily git is great.
Firstly, you want to move to your feature branch, so do git checkout -b name-of-new-branch, tis'll create a new branch with the 2 new commits.
Then switch to master with git checkout master, and look at the logs, with git log. You'll see your 2 new commits that you want to 'get rid of'. Look for the commit just before the 2 commits, and look at the commit: … line. Copy & paste the part after the :, it'll look something like: 09c171332d969c55bceb80db58c70a710be994e3.
This is the id of a commit, (and tree of files), and it can act like a branch. You can checkout that 'branch' with git checkout 09c171332d969c55bceb80db58c70a710be994e3, which will give you a few lines message about how it's a "detached HEAD". Once it's checked out, give this a name, with git checkout -b master2. We are calling this new branch, master2. It's got everything in master, but without the 2 commits which want to have on the feature branch. Then just delete the old master branch (git branch -d master), and rename master2 to master (git branch -m master2 master).
master will now have everything but without the last 2 commits, and name-of-new-branch will have master and the 2 commits, so you can continue work there.
Obviously things get a bit more complicated if you've pushed master somewhere or if you have a lot of local changes. Best to not do it in that case.
Published on
Tags:
Previous Post: ← Using scipy with Django mod_wsgi Next Post Undo the local changes in a git file →