Git:如何自动合并本地分支与远程跟踪而无需提取

发布于 2024-09-29 06:30:09 字数 283 浏览 4 评论 0原文

想象一下,我有几个分支:master、a、b、c ...

现在我在 master 分支和“git pull”中。这会将所有更改从远程服务器获取到 origin/master、origin/a、origin/b ...分支,并将 CURRENT 分支(master)与 origin/master 合并。但随后我想切换到 A 分支,并再次合并来自远程跟踪分支的这些远程更改,而无需再次获取更改(因为它们已经位于 origin/a 分支中)。

是否有一些更简单的方法可以做到这一点,而不是精确指定我跟踪的远程分支(即自动)?

Imagine, I have several branches: master, a, b, c ...

Now I'm in master branch and "git pull". This fetches all changes from remote server into origin/master, origin/a, origin/b ... branches and merges CURRENT branch (master) with origin/master. But then I want to switch to A branch and again merge these remote changes from remote tracked branch, WITHOUT fetching changes again (as they already are in origin/a branch).

Is there some simplier way to do this not specifying exactly remote branch I track (i.e. automatically) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

韬韬不绝 2024-10-06 06:30:09

我不相信有一个内置命令可以做到这一点。但是,您可以这样做:

#!/bin/bash
head=$(git symbolic-ref HEAD) || exit
head=${head#refs/heads/}
merge=$(git config --get branch.$head.merge) || { echo "no tracking branch"; exit 1; }
remote=$(git config --get branch.$head.remote) || remote=origin
git merge $remote/${merge#refs/heads/}

# alternatively, as in Aristotle's answer:
# head=$(git symbolic-ref HEAD) || exit
# upstream=$(git for-each-ref --format='%(upstream:short)' "$head"
# [ -z "$upstream" ] && { echo "no tracking branch"; exit 1; }
# git merge $upstream

我想我已经很好地涵盖了我的基础 - 如果处于分离的 HEAD 状态,或者当前分支没有跟踪分支,它会以失败状态退出。它默认为 origin,就像普通的 git 传输命令一样。 (如果您尝试在跟踪远程分支以外的其他内容的分支上使用它,就会发生奇怪的事情,即不是 refs/heads/* 形式,但这似乎不太可能。) 看起来不是这样实际上可以节省你很多时间,但你就在这里!

如果你想使用它,只需将它存储在某个地方并为其添加别名,或者将其命名为 git-something 并将其放入你的 PATH 中。

I don't believe there's a built-in command for this. However, you could do something like this:

#!/bin/bash
head=$(git symbolic-ref HEAD) || exit
head=${head#refs/heads/}
merge=$(git config --get branch.$head.merge) || { echo "no tracking branch"; exit 1; }
remote=$(git config --get branch.$head.remote) || remote=origin
git merge $remote/${merge#refs/heads/}

# alternatively, as in Aristotle's answer:
# head=$(git symbolic-ref HEAD) || exit
# upstream=$(git for-each-ref --format='%(upstream:short)' "$head"
# [ -z "$upstream" ] && { echo "no tracking branch"; exit 1; }
# git merge $upstream

I think I've covered my bases pretty well - it exits with failure status if in detached HEAD state, or if the current branch doesn't have a tracking branch. It defaults the to origin, as do the normal git transfer commands. (Something weird will happen if you try to use it on a branch which is tracking something other than a branch on the remote, i.e. not of the form refs/heads/*, but that seems pretty unlikely.) Doesn't seem like it'd actually save you much time, but there you are!

If you want to use it, just store it somewhere and alias to it, or name it git-something and put it in your PATH.

旧时光的容颜 2024-10-06 06:30:09

我认为您可以简单地签出您的本地分支并从原始/分支的本地副本进行合并。

像这样的东西:

git checkout a
git merge origin/a

I think you can simply checkout your local branch and merge from your local copy of the origin/a branch.

Something like:

git checkout a
git merge origin/a
朮生 2024-10-06 06:30:09

git pull 只不过是 git fetch + git merge。使用参数 --rebase 你也可以让它变基而不合并,这样你就可以切换到另一个分支并合并/变基:

git checkout a
git merge origin/a

或者:

git checkout a
git rebase a

因为它没有做任何其他事情,我什至已经停止了完全使用拉力。我只是定期git fetch,这样我就可以轻松跟踪其他存储库中的更改,然后在准备好时git merge/rebase

git pull is nothing more than git fetch + git merge. With the parameter --rebase you can also have it rebase and not merge so you can just switch to the other branch and merge/rebase:

git checkout a
git merge origin/a

or:

git checkout a
git rebase a

Since it doesn't do anything else I even have stopped using pull at all. I just git fetch regularly so I can track the changes in other repositories easily and then git merge/rebase whenever I'm ready.

提笔书几行 2024-10-06 06:30:09

这将依次检查所有跟踪分支,并合并到相应的远程分支中:

git for-each-ref --format='%(refname:short) %(upstream:short)' \
| while read branch upstream ; do
    [ -z "$upstream" ] && continue
    git checkout "$branch"
    git merge "$upstream" || git reset --hard
done

如果合并有冲突,则使用 git reset 进行倒回,您必须手动执行此操作。

未经测试。

您还可以将一个或多个模式传递给 git for-each-ref ,以将其限制为某个分支或一组特定分支。

This will check out all tracking branches in turn, and merge in their corresponding remote branches:

git for-each-ref --format='%(refname:short) %(upstream:short)' \
| while read branch upstream ; do
    [ -z "$upstream" ] && continue
    git checkout "$branch"
    git merge "$upstream" || git reset --hard
done

If a merge has conflicts, it is rewound with git reset and you will have to do it manually.

Untested.

You can also pass one or more patterns to git for-each-ref to restrict it to a certain branch or a certain set of them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文