简体   繁体   中英

Git pull from master branch being in other branch

I created a new branch from the master and changed few files. How can i pull fresh files from master being in newly created branch and ignore/overwrite changed files in current branch?

You can undo your local changes and restore the files as they're in the master branch by saying:

git checkout master -- .

where the -- tells Git to interpret whatever comes next as a list of paths and the . indicates all paths.


If you're using Git 2.23 (August 2019) or later, you can use the new (but still experimental ) git restore command:

git restore master -- .

From the release notes :

Two new commands "git switch" and "git restore" are introduced to split "checking out a branch to work on advancing its history" and "checking out paths out of the index and/or a tree-ish to work on advancing the current history" out of the single "git checkout"

Essentially, the two things that git checkout does have now received dedicated commands:

  • git switch to check out a branch and update the working directory accordingly
  • git restore to check out files from a specific commit (or the index) in the working directory

There are a lots of ways to do it. Some are shown below.

  • If you want your current branch to be exactly same as your local master branch, you should just run the following command. This will remove all your changes.

     $ git reset --hard master
  • If you want your current branch to be exactly same as your remote master branch, you should just run the following commands. We should fetch the remote branches just to be sure that they are all synced.

     $ git fetch origin $ git reset --hard origin/master
  • Another way is to delete the current branch and create a new branch from master with the same name. Before doing so, if you have some uncommitted data, you should stash them or make a commit.

    • To stash the uncommitted data, run the following command.

       $ git add.; git stash; git stash drop
    • Or to commit the changes, run the following command.

       $ git add.; git commit -m "just commit & it won't be after a while"

    And finally delete and create the branch by running the following command.

     $ git checkout master $ git branch -D <branch-name> $ git branch <branch-name>

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