Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a git branch equal to master again

Tags:

git

branch

Short version

What is the easiest way to make a branch equal to master again, discarding any differences? Up until now, I simply used git branch -D wip, followed by git checkout -b wip, but that seems a bit silly.

Motivation

I often have a wip branch alongside my master branch, for 'work in progress'. Sometimes the work on such a branch gets 'left behind' and when I rediscover it, I find I no longer want it. This rediscovery often happens when I want to use the branch to store a new set of 'work in progress' and I find I simply want to discard any differences with master, so the new 'work in progress' fits on top. Figuring out the differences is not worth the trouble: the solution described above works fine for me. Any better solutions to address this use case?

like image 357
Confusion Avatar asked Nov 21 '12 09:11

Confusion


2 Answers

try this one in your branch :

git reset --hard master

This will make your branch identical to master, remove all commits you may have done, and discard all the files in the staging area.

like image 186
Intrepidd Avatar answered Nov 02 '22 00:11

Intrepidd


If you really want to blow away the previous branch with that name, and create a new one you could just use:

git checkout -B wip master

Using the capital version rather than -b will cause git to switch to the named branch, and reset it to the new starting point named as the last argument. If you're currently at the desired starting point you could leave off the last argument (master here), it will default to using HEAD.

like image 39
qqx Avatar answered Nov 02 '22 00:11

qqx