简体   繁体   中英

GIT: How to add a forgotten commit (in master) to a new branch

An hour ago, I created a new branch (spawned from the master branch) and pushed it to the remote. However, a co-worker forgot to commit/push his local changes, so he just committed it now into the master branch. How do I ensure that this commit is also reflected in the new branch I created?

Fetching the remote branch and rebasing your branch on it is the easiest way to, ie.

git fetch origin
git checkout <branch>
git rebase origin/master

For more details see: https://git-scm.com/book/de/v2/Git-Branching-Rebasing

Your local repository and the remote look like this.

origin
A - B - C - G - H [master]
         \
          E - F [feature]

local
          [master]
A - B - C [origin/master]
         \
          E - F [origin/feature]
                [feature]

G and H are the commits to master your co-worker pushed, but you haven't fetched yet.

First, update your master with a git pull . This will fetch any changes and merge the into your local master branch.

$ git checkout master
$ git pull

origin
A - B - C - G - H [master]
         \
          E - F [feature]

local
                  [master]
A - B - C - G - H [origin/master]
         \
          E - F [origin/feature]
                [feature]

Then update your branch with the latest commits from master by merging.

$ git checkout branch
$ git merge master

origin
A - B - C - G - H [master]
         \
          E - F [feature]

local
                  [master]
A - B - C - G - H [origin/master]
         \       \
          E - F - M [feature] (at merge commit M)
              [origin/feature] (still at commit F)

Then git push your changes.

$ git push

origin
                  
A - B - C - G - H [master]
         \       \
          E - F - M [feature]

local
                  [master]
A - B - C - G - H [origin/master]
         \       \
          E - F - M [feature]
                    [origin/feature] (now at M)

Alternatively, you can rebase your work. This is a little more complex, but the results are better.

Instead of merging your commits will be replayed on top of the latest master.

$ git checkout branch
$ git rebase master

origin
A - B - C - G - H [master]
         \
          E - F [feature]

local
                  [master]
A - B - C - G - H [origin/master]
         \       \
          \        E1 - F1 [feature]
           \
            E - F [origin/feature]

Because feature and origin/feature have "diverged" a git push will result in an error. Instead, use git push --force-with-lease to safely push the divergent changes.

origin
A - B - C - G - H [master]
                 \
                   E1 - F1 [feature]

local
                  [master]
A - B - C - G - H [origin/master]
                 \
                  E1 - F1 [feature]
                          [origin/feature]

Now there is no "update" merge. Many update merges can clog up the history making it difficult to tell what was done in the branch.

If others on the project try to git pull your branch they will get an error because the new commits are not simple children of the branch. They will have to pull your branch with git pull --rebase . I recommend this. You can make it the default with git config --global pull.rebase merge .

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