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

Time:01-10

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:[email protected]/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?

CodePudding user response:

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 <URL>
git fetch

# for branches that already exist locally:
git switch <branch>
git branch -u origin/branch

# for remote branches not checked out locally:
git switch <branch>  # will automatically take 'origin/<branch>' 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 <repo> "$(git branch --show-current)"

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

  • Related