简体   繁体   中英

How do I use GIT to completely reset the 'master' branch to the contents of 'dev'?

Git is something that I have always had difficulties comprehending. A common mistake that I often make when working on a personal project is committing work to 'master' instead of 'dev'.

I would like to know how to use Git 'reset' the master branch. Specifically, if 'master' is ahead of the 'dev' branch by some number of commits, how do I reset 'master' to the contents of 'dev'. This is assuming the commits in master are not needed.

Thank you.

So, if the commits in master are really not needed, the git reset answers are correct (well, assuming you have not pushed master yet).

However, you say:

A common mistake that I often make when working on a personal project is committing work to 'master' instead of 'dev'.

If this is the case, why throw away the commits you made on master ? I would just swap the branches (if master is strictly ahead of dev ):

git checkout master
git branch temp
git reset --hard dev
git checkout dev
git reset --hard temp
git branch -d temp

You're asking how to "reset" the master branch. Good news for you,

git checkout master
git reset --hard dev # loses commits on master

Assuming you are on the master branch, this command will set the master branch to match the dev branch.

git reset --hard dev

Please note that this will erase any of the work that you have committed in the master branch that is not included in the dev branch. If you want to save the state of the files that you have changed in the master branch but remove those commits in master, you can run this command which will reset the commits to the current state of dev but will not remove the changes that you have made to the files locally. These changes can then be committed.

git reset --soft dev

master is only the default branch name in git(in fact, only a reference), why bother with commit to it. you can rename it to any other name, and create a new branch named master from any start-pointer as you will.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM