Home > Mobile >  Specify branch name during git pull to avoid pulling from default branch
Specify branch name during git pull to avoid pulling from default branch

Time:08-06

I have below conditions my .yml file that is run to do the deployment to windows server.

  1. If folder 'ABC' does not exist in windows server, then create a folder 'ABC' and execute below command

git clone --branch master https://[email protected]/username/repo.git master\

  1. If folder 'ABC' already exists, then just do a pull using below command to pull latest changes

git pull https://[email protected]/username/repo.git

Problem is, when it executes step 2, instead of pulling from 'master' branch, code is being pulled from 'Dev' branch since 'Dev' is the default branch in github (I would not want to change the default branch) and we are not specifying branch in the git pull command.

Can you please help me with the single git pull command (as in step 2) in which we specify the branch name as well. This needs to be a single pull command since .yml script is allowing to use only one command here.

I tried below options and these threw errors saying git command is invalid

git pull --branch master https://[email protected]/username/repo.git master\

git pull origin master https://[email protected]/username/repo.git

CodePudding user response:

The error is perfectly valid. You have specified origin as well as the Remote URL.

The command that you need:

  • git pull REMOTE_URL BRANCH_NAME

origin will only work if the git config has an origin setup. You can consider origin to be a variable that holds the URL to the Remote Repo. (You usually set it when you are initialising the Git repo)

Note:

  • git pull --ff-only can be used if you want the merge to be fast-forwarded.

What if there is a conflicting history?

git pull --ff-only won't work in that case and since, this command is going to be a part of an automated deployment, you can't manually resolve the conflicts. In this case use:

  • git fetch REMOTE_URL BRANCH_NAME && git reset --hard FETCH_HEAD

This will perform a git fetch followed by a hard reset.

  • Related