如何配置Git在使用"git pull"命令时自动从当前分支拉取代码?

56

在我们目前的设置中,当执行拉取操作时,您总是需要输入分支名称(例如:git pull origin feature-branch)。我已经犯过一次错误,将一个分支合并到了另一个分支中,导致两个不同版本的分支被错误地合并了。为了避免这种情况发生,我希望通过配置 Git,使得简单地输入 git pull 命令可以拉取您当前所在的分支。

我应该如何做到这一点?

8个回答

43

我也是一个喜欢只输入 git pull 就能完成一切的粉丝。

你有两个选项:

1) git config --global branch.autoSetupMerge always

这将确保无论你是检出远程分支还是创建新分支,git都会自动处理跟踪信息,然后你就可以使用

git clone <some_repo>
git checkout -b <new_branch>
git push
git pull
请注意,为了无需更多关键字就能push,您需要设置push选项。我已将其设置为matching,但每个人在此方面都有自己的喜好。(使用git config --global push.default matching)。 更多信息: autosetupmerge默认为true。当设置为true时,这使得git在您检出远程已存在的分支时执行跟踪操作。例如,如果您执行git checkout <branch>,git会处理跟踪信息,以便您在该分支上执行git pull。然而,它不会在您使用-b选项创建的分支上执行此操作。将autosetupmerge设置为always可确保git始终处理跟踪信息。 2)在检出新分支时,您需要明确设置要从origin拉取的分支(即跟踪分支)。
git checkout -b <branch> --track <remote>/<branch>

如果分支是短暂的,我认为这个方法不太有用。如果你很少创建新的分支,你应该选择这种方法。但是,如果像我一样,只有分支是持久的,每个功能都有自己全新的分支,那么我认为选项1更有用。

请注意,您不需要将git配置 --global。您可以在那里简单地写入--local,并使该设置仅特定于该存储库。


13
“git config --global branch.autoSetupMerge always” 是我一直在寻找的命令。谢谢。我不明白为什么这不是默认设置。 - sudo
请注意,branch.autoSetupMerge只在创建新分支时有效,即通过git checkout -b ...命令。如果分支已经存在,则该命令无效,这种情况下唯一的解决方案是使用git --set-upstream-to ...命令。 - gented
如果您使用的是浅克隆,那么 git pullgit pull origin yourBranch 是不同的。 - BollMose
branch.autoSetupMerge always 是危险的:当你从本地分支创建分支时,它也会设置跟踪,这可能不是你想要的。对于大多数人来说,默认值 branch.autoSetupMerge true 是有意义的(如果你要创建的分支是远程分支,则设置跟踪),或者 branch.autoSetupMerge simple(仅在你要创建的本地分支与你要创建的远程分支具有相同名称时才设置跟踪)。 - Tao

15

这对我有用:

git branch --set-upstream-to=origin/branch_name branch_name

完成这个步骤后,我可以使用以下语法:

git checkout branch_name
git pull --rebase
git push

7
git branch -u origin/branch_name 简写为: - Two-Bit Alchemist

12
你可以创建一个跟踪分支。根据Git Book (http://git-scm.com/book/en/Git-Branching-Remote-Branches)的介绍:

When you clone a repository, it generally automatically creates a master branch that tracks origin/master. That’s why git push and git pull work out of the box with no other arguments. However, you can set up other tracking branches if you wish — ones that don’t track branches on origin and don’t track the master branch. The simple case is the example you just saw, running git checkout -b [branch] [remotename]/[branch]. If you have Git version 1.6.2 or later, you can also use the --track shorthand:

$ git checkout --track origin/serverfix
Branch serverfix set up to track remote branch refs/remotes/origin/serverfix.
Switched to a new branch "serverfix"

9
如果你已经配置了git push自动获取远程分支名称,那么你可以使用-u参数,并且它会自动设置跟踪分支。
要设置 git push 使用相同名称的远程分支,请执行以下操作:
git config --global push.default current`

翻译来自git help push-u选项的说明:

-u, --set-upstream
    For every branch that is up to date or successfully pushed, add
    upstream (tracking) reference, used by argument-less git-pull(1) and
    other commands. For more information, see branch.<name>.merge in
    git-config(1).

假设您当前所在的分支是 <branch_name>
$ git push -u
Branch <branch_name> set up to track remote branch <branch_name> from origin.
Everything up-to-date

9
问题不是关于git push,而是关于git pull吧? - iainbeeston
1
@iainbeeston 这并不重要。这有助于为 git pull 设置当前分支。 - user2976612

1

我也喜欢简短命令的易用性,减少人为错误。可惜的是,git pull 没有可配置的默认选项。不幸的是,最佳答案似乎不能在已经检出的分支上运行(如果你在拉取了一堆仓库之后才查找此答案,这会很麻烦),这迫使你在每个仓库的每个现有分支上运行 git branch --set-upstream-to=origin/<branch> <branch>

为了减少输入字符数,你可以使用 git alias 实现所有分支的快捷拉取,并将其命名为你远程所需命名的分支. 这里有一个创建 git pu 别名的示例:

git config --global alias.pu '!git pull $(git remote) $(git branch --show-current)'

现在每次输入git pu,它都会自动展开为完整的pull命令。

Shell扩展非常强大,如果您喜欢使用git pull,您还可以创建一个别名快速更新远程分支跟踪,例如git up

git config --global alias.up '!git branch --set-upstream-to=$(git remote)/$(git branch --show-current) $(git branch --show-current)'

0

我需要将我的存储库与主分支同步,因此我最终使用了一个简单的Bash脚本来获取更改并将其重新基于主分支:

function git_do_rebase_with_master (){
    current=$(pwd)
    echo "Syncing $1 ..."
    cd "$1"
    git fetch origin
    GIT_STASH_MESSAGE="Sync on $(date)"
    echo $GIT_STASH_MESSAGE
    git stash -m"${GIT_STASH_MESSAGE}"
    git rebase origin/master
    (git stash list | grep "${GIT_STASH_MESSAGE}" && git stash pop) || echo "Stash was not applied"
    echo "Completed git sync current branch"
    git log --name-status HEAD^..HEAD --pretty=oneline -1
    echo "Completed syncing of $1 ..."
    cd $current
}


alias sync_repo="git_do_rebase_with_master /path/to/repo" 

-3
此外,如果您进入您的.gitconfig文件并进行一些小更改,您可以将其设置为自动假定您想要从任何项目中推送/拉取当前分支。因此,使用您喜欢的任何编辑器打开.gitconfig。找到[push]选项,将其设置为default=simple,就像下面这样。
[push]
        default = simple
[pull]
        default = simple

就像那样。将“pull”也改为“simple”。它们两个现在可能都设置为“current”。本质上,这与我之前发布的选项完全相同:

git config --global pull.default current

但我发现这是更好的选择。因此,您可以使用相同的代码行,但将current改为simple


1
这个解决方案不起作用,它给了我以下错误: 错误:push.default的值格式不正确:simple 错误:必须是nothing、matching、tracking或current之一。 致命错误:/home/rebecca/.gitconfig中第11行的配置文件有误。 - Rebecca Meritz
请从命令行运行以下代码: 'git config --global push.default simple' 'git config --global pull.default simple' - Zaichik
更新:它在Git 2.3中不起作用,但在Git 2.4中可以正常工作。 - jbustamovej

-13

这个命令应该配置git在终端/iTerm中运行时拉取到当前分支。

git config --global push.default current

我认为你可以将“push”改为“pull”,以获得与“push”相同的效果。


5
那么,拉力问题的解决方案是什么? - biniam
12
这简直是胡说八道。在配置帮助文档中没有记录 pull.default - Rylee Fowler
我认为 git branch -u origin/<branch_name> 会产生类似的效果。 - Zaichik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接