简体   繁体   中英

How to copy all changes from the remote origin master branch to a new branch on local by discarding all changes of it

I have a local master branch with 5 commits ahead of remote origin master. I want to create a new branch 'fresh-master' on my local with discarding all changes in my local master branch. and copy exact same code of remote origin master. While I want to keep the changes in the local master. How should I do it? I have created a new branch by git branch fresh-master .

git fetch
git branch fresh-master origin/master

will create a new fresh-master branch from the remote state of the branch, without any impact on the already existing master .

The new branch will not contain master recent changes (your recent unpushed commits).

RomainValeri's answer is mostly correct, but doesn't account for the fact that you said you've already created the new branch. In that case, the easiest thing to do is

git checkout fresh_master
git reset --hard origin/master

You may also want to set origin/master up as the upstream branch for fresh_master.

git branch -u origin/master

However , it might get confusing having two branches tracking the same remote branch; it is in some ways more straightforward if you instead create a new branch at your local master, and then simply reset master to origin/master preserving the normal tracking relationships.

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