git 拉,变基,推,在一个命令(或只是几个)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7555800/
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-19 06:01:41  来源:igfitidea点击:

Pull, rebase, push, in one command (or just a few)

gitmergerebasegit-rebase

提问by Fred Foo

When using Git, I often find myself doing the following when working in master:

在使用 Git 时,我经常发现自己在使用 Git 时执行以下操作master

# work work work...
$ git checkout -b temp
$ git commit -a -m 'more work done'
$ git checkout master
$ git pull origin master
# turns out master was updated since my previous pull
$ git checkout temp
# I don't want a merge commit for a simple bugfix
$ git rebase master
$ git checkout master
$ git merge temp
$ git push origin master
$ git branch -d temp

... and I get tired of doing this. Is there a way to do this dance without all of the checkouts, and preferably without (manually) creating the temporary branch?

......我厌倦了这样做。有没有办法在没有所有结帐的情况下进行这种舞蹈,最好没有(手动)创建临时分支?

回答by Mark Longair

If you don't mind not creating a branch called temp, you could just do the following all on master:

如果您不介意不创建一个名为 的分支temp,您可以在 上执行以下所有操作master

git commit -a -m 'more work done'
git fetch origin
git rebase origin/master

... or equivalently:

...或等效地:

git commit -a -m 'more work done'
git pull --rebase origin master

If you do want to keep the tempbranch, however, you can still make this a bit shorter by not checking out masterjust to do the pull- you only need to fetchand then rebase your branch onto origin/master:

temp但是,如果您确实想保留分支,您仍然可以通过不签出master只是为了执行此操作来缩短此时间pull- 您只需要fetch然后将您的分支重新设置为origin/master

# work work work...
$ git checkout -b temp
$ git commit -a -m 'more work done'
$ git fetch origin
# It looks like origin/master was updated, so:
$ git rebase origin/master
# Then when you finally want to merge:
$ git checkout master
$ git merge temp
$ git push origin master
$ git branch -d temp

sehe's answerreminds me that you could replace:

sehe的回答提醒我你可以替换:

$ git fetch origin
$ git rebase origin/master

... with:

... 和:

$ git pull --rebase origin master

... which is nearlyequivalent. The difference is that when you run git fetch origin, all of your remote-tracking branches for originwill be updated, whereas when you pull a particular branch from origin, none of them are - it's just the temporary ref FETCH_HEADthat is updated. I personally prefer running one extra command (git fetch origin), and seeing all the remote branches that have changed in the output.

...这几乎是等价的。不同之处在于,当您运行 时,您的git fetch origin所有远程跟踪分支origin都将被更新,而当您从 中拉出特定分支时origin,它们都没有 - 只是FETCH_HEAD更新的临时引用。我个人更喜欢运行一个额外的命令 ( git fetch origin),并在输出中查看所有已更改的远程分支。

回答by sehe

You can at least optimize the rebasing: git pull --rebase

您至少可以优化变基: git pull --rebase

I'm not exactly sure how you like things, but I like my workflow sort of like this:

我不确定你喜欢什么东西,但我喜欢我的工作流程是这样的:

git checkout -b temp
git commit -a -m 'more work done'
git pull --rebase origin master

That way I keep my focus on the branch at hand.

这样我就可以将注意力集中在手头的分支上。

Then later, I do

后来,我做

git checkout master
git pull origin master
git merge temp

etc.

等等。