git 如何将分支快进到头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9512549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:04:46  来源:igfitidea点击:

How to fast-forward a branch to head?

git

提问by pengguang001

I switched to master after developing on a branch for a long time. The log shows:

在一个分支上开发了很长时间后,我转向了master。日志显示:

Your branch is behind 'origin/master' by 167 commits, and can be fast-forwarded.

您的分支落后于 'origin/master' 167 次提交,并且可以快进。

I tried:

我试过:

git checkout HEAD

It has no effect. This is because I have checkout an intermediate commit on master.

它没有效果。这是因为我在 master 上签出了一个中间提交。

How to make master stay on head?

怎么让师父保持头脑清醒?

采纳答案by Greg Hewgill

Doing:

正在做:

git checkout master
git pull origin

will fetch and merge the origin/masterbranch (you may just say git pullas origin is the default).

将获取并合并origin/master分支(您可能只是说git pullorigin 是默认值)。

回答by rob mayoff

Try git merge origin/master. If you want to be sure that it only does a fast-forward, you can say git merge --ff-only origin/master.

试试git merge origin/master。如果你想确保它只进行快进,你可以说git merge --ff-only origin/master.

回答by voidvector

In your situation, git rebasewould also do the trick. Since you have no changes that master doesn't have, git will just fast-forward. If you are working with a rebase workflow, that might be more advisable, as you wouldn't end up with a merge commit if you mess up.

在您的情况下,git rebase也可以解决问题。由于您没有 master 没有的更改,因此 git 只会快进。如果您正在使用 rebase 工作流,这可能更可取,因为如果您搞砸了,您最终不会得到合并提交。

username@workstation:~/work$ git status
# On branch master
# Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
#   (use "git pull" to update your local branch)
#
nothing to commit, working directory clean
username@workstation:~/work$ git rebase
First, rewinding head to replay your work on top of it...
Fast-forwarded master to refs/remotes/origin/master.
# On branch master
nothing to commit, working directory clean

回答by Kuba

git checkout master
git pull

should do the job.

应该做的工作。

You will get the "Your branch is behind" message every time when you work on a branch different than master, someone does changes to master and you git pull.

每次在与 master 不同的分支上工作时,您都会收到“您的分支落后”消息,有人对 master 进行了更改,而您 git pull 。

(branch) $ //hack hack hack, while someone push the changes to origin/master
(branch) $ git pull   

now the origin/master reference is pulled, but your master is not mergedwith it

现在原点/主参考被拉出,但你的主没有与它合并

(branch) $ git checkout master
(master) $ 

now master is behind origin/masterand can be fast forwarded

现在master 落后于 origin/master并且可以快进

this will pull and merge (so merge also newer commits to origin/master)
(master) $ git pull 

this will just merge what you have already pulled
(master) $ git merge origin/master

now your master and origin/master are in sync

现在你的主人和原点/主人是同步的

回答by jontro

If you are standing on a different branch and want to checkout the newest version of master you can also do

如果您站在不同的分支上并想签出最新版本的 master,您也可以这样做

git checkout -B master origin/master

git checkout -B master origin/master

回答by Sunny Patel

To anyone that wants to Fast Forward they are not onto another remote branch (including itself) without checking out that branch, you can do:

对于想要快进的任何人,如果不检查该分支,他们就不会进入另一个远程分支(包括它自己),您可以这样做:

git fetch origin master:other

This basically fast forwards the index of otherto origin/masterif you are not on otherbranch. You can fast forward multiple branches this way.

如果您不在分支上,这基本上会快进otherto的索引。您可以通过这种方式快进多个分支。origin/masterother

If you working on another branch for some time, and wanted to update stale branches from remote to their respective head:

如果您在另一个分支上工作了一段时间,并且想将过时的分支从远程更新到它们各自的头:

git fetch origin master:master other:other etc:etc

回答by Hasan Junaid Hashmi

No complexities required just stand at your branch and do a git pullit worked for me

不需要复杂性,只需站在你的分支上做一个git pull它对我有用

Or, as a second try git pull origin masteronly in case if you are unlucky with the first command

或者,作为第二次尝试git pull origin master以防万一您对第一个命令不走运

回答by bobah

To rebasethe currentlocal trackerbranch moving local changes on top of the latest remote state:

变基当前本地跟踪器分支移动最新的远程状态的顶部局部变化:

$ git fetch && git rebase

More generally, to fast-forwardand drop the local changes (hard reset)*:

更一般地,要快进并删除本地更改(硬重置)*:

$ git fetch && git checkout ${the_branch_name} && git reset --hard origin/${the_branch_name}

to fast-forwardand keep the local changes (rebase):

快进和保持局部变化(衍合):

$ git fetch && git checkout ${the_branch_name} && git rebase origin/${the_branch_name}

* - to undo the change caused by unintentional hard reset first do git reflog, that displays the state of the HEAD in reverse order, find the hash the HEAD was pointing to before the reset operation (usually obvious) and hard reset the branch to that hash.

* - 要撤消由意外硬重置引起的更改,首先执行git reflog以相反顺序显示 HEAD 的状态,找到重置操作之前 HEAD 指向的散列(通常很明显)并将分支硬重置为该散列。

回答by Do Async

Move your branch pointer to the HEAD:

将分支指针移动到 HEAD:

git branch -f master

Your branch masteralready exists, so git will not allow you to overwrite it, unless you use... -f(this argument stands for --force)

你的分支master已经存在,所以 git 不允许你覆盖它,除非你使用... -f(这个参数代表--force

Or you can use rebase:

或者您可以使用变基:

git rebase HEAD master

Do it on your own risk ;)

风险自负;)