Git 在从 Bash 文件中拉取分支时显示 ‘up-to-date’。

huangapple go评论73阅读模式
英文:

Git showing 'up-to-date' when pulling branch from bash file

问题

在bash脚本中,您可以使用以下方式来确保在脚本中执行git pull时,从当前分支拉取更新:

#!/bin/bash

current_branch=$(git rev-parse --abbrev-ref HEAD)
echo "Current branch: $current_branch"

echo "git pull ..."
git pull origin "$current_branch"

这段脚本首先获取当前分支的名称,然后使用git pull origin命令从当前分支拉取更新。这样,无论您在哪个分支上运行脚本,它都会自动拉取该分支的更新。

请注意,origin是默认的远程仓库名称,如果您的远程仓库有不同的名称,请将其替换为实际的远程仓库名称。

英文:

I have a bash script that pulls and builds some source code. If I am on the master branch, it works as expected, however if I change to a different branch it says it is already up to date even if there are pushed changes. If I do a git pull outside the script from the cmd line, it pulls as expected.

deploy.sh

echo | git branch
echo "git pull ..."
git pull https://tech-dev:password@bitbucket.org/mycompany/pow-wow.git

Output

./deploy.sh

  master
* spring3upgrade
git pull ...
From https://bitbucket.org/mycompany/pow-wow
 * branch            HEAD       -> FETCH_HEAD
Already up-to-date.

Question

How do I get it to pull from the branch I am currently on in the bash script?

答案1

得分: 2

为什么它不起作用

如果您使用显式URL进行拉取(如在您的问题中显示的):

  • 没有默认的refspec,因此只会拉取远程的HEAD(默认分支)。
  • 您检出的分支没有默认的“远程分支”配置,因此git pull将合并默认分支所指向的内容(即它将尝试合并origin/master而不是origin/spring3upgrade)。

如何修复

最简单的方法是定义一个命名的远程(例如:origin),让Git设置其默认配置,并拥有命名的远程跟踪分支:

git remote add origin <URL>
git fetch

# 对于已经存在于本地的分支:
git switch <branch>
git branch -u origin/branch

# 对于未在本地检出的远程分支:
git switch <branch>  # 将自动以'origin/<branch>'作为基础,并将其设置为上游分支

如果您有特殊需求需要不命名远程:您可以在命令行上提供所需的refspec,可能是:

# 拉取与本地分支同名的分支:
git pull <repo> "$(git branch --show-current)"

您需要提供特定的凭据来访问远程仓库。有许多方法可以实现这一点:

  • 一个相当常见的方法是通过SSH:创建一个SSH密钥,配置您的中央服务器以接受用于CI(您可以选择的名称...)专用用户的公钥,并设置您的构建代理通过该密钥访问您的仓库。

  • 或者使用HTTPS,设置凭据管理器(参见您在评论中发布的链接),或在git帮助配置中的许多http.*设置中进行配置。

英文:

why it doesn't work

If you are pulling using an explicit URL (as displayed in your question) :

  • there is no default refspec, so only the remote HEAD (the default branch) is fetched
  • there is no default "remote branch" configured for your checked out branch, so git pull will merge in whatever that default branch points to (ie: it will try to merge origin/master, not origin/spring3upgrade)

how to fix it

The simplest way is to define a named remote (e.g: origin), let git set up its default configuration, and have named remote tracking branches:

git remote add origin &lt;URL&gt;
git fetch

# for branches that already exist locally:
git switch &lt;branch&gt;
git branch -u origin/branch

# for remote branches not checked out locally:
git switch &lt;branch&gt;  # will automatically take &#39;origin/&lt;branch&gt;&#39; as a base,
                     # and set it as the upstream branch

If you have a special need which requires to not name the remote: you may provide the refspec you need on the command line, probably:

# pull the branch which has the same name as your local branch:
git pull &lt;repo&gt; &quot;$(git branch --show-current)&quot;

You need to provide specific credentials to access your remote. There are many ways to do that :

  • a pretty common way is to go through ssh: create an ssh key, configure your central server to accept the public key for a CI (you choose the name ...) dedicated user, and set up your builder agent to access your repo through ssh with that key

  • or using https, set up a credentials manager (see the link your posted in your comment, or git help credentials), or the many http.* settings in git help config

huangapple
  • 本文由 发表于 2023年1月9日 19:00:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定