1

I am creating a shell build script.

I want it to be called like this:

./build.sh REPONAME BRANCHNAME

$REPONAME corresponds to a remote. I am assuming that the remote exists. $BRANCHNAME is a branch existing on $REPONAME.

$BRANCHNAME may have never been checked out on this computer.

So I have this:

cd $REPOPATH
git fetch $REPONAME $BRANCHNAME
git checkout --track $REMOTE/$BRANCHNAME
git pull $REPONAME
echo `git rev-parse HEAD`

This kind of works but I am experiencing several issues:

  • I have to enter my key's private key twice - I guess once for fetch and once for pull. Not critical but if it would be just once, would be better.
  • Because of --track, if $BRANCHNAME was already checked out in the past, I get

fatal: A branch named $BRANCHNAME already exists

. No idea how fatal that really is but I don't like to see a fatal in the output

  • Without the --track, I get into detached mode, which I also do not like
  • With just git checkout $BRANCHNAME I was suddenly getting messages about "ambiguous" branches. Maybe because $BRANCHNAME could be on several remotes?

So what is the cleanest and unequivocal way to checkout a branch from a remote, get its latest version and build from it?f

It's astonishing how after so many years using git I still don't have a grasp of what feels like must-know skills.

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
transient_loop
  • 5,984
  • 15
  • 58
  • 117

1 Answers1

1

If you want to discard everything and get the remote version :

git fetch $REMOTE
git stash
git checkout $BRANCHNAME
git branch -u $REMOTE/$BRANCHNAME
git reset --hard $REMOTE/$BRANCHNAME

If you want to merge the updates from the remote branch in your local branch :

git fetch $REMOTE
git stash
git checkout $BRANCHNAME
git branch -u $REMOTE/$BRANCHNAME
git merge $REMOTE/$BRANCHNAME
LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • This looks very promising. Thank you. – transient_loop Aug 12 '20 at 21:25
  • I usually would not have any changes on that branch so the first suggestion looks the most appropriate. – transient_loop Aug 12 '20 at 21:26
  • Some of the actions in my first answer could fail (for example, if `$BRANCHNAME` is your active branch `git branch -f $BRANCHNAME ...` will fail). Edited my answer with something that will perhaps print some warnings such as `Already on '$BRANCHNAME'` but should work. – LeGEC Aug 13 '20 at 06:38
  • I will accept this as this is closes to what I asked. I am currently asking myself though if I just should better clone the repo everytime. Could work for my use-case. But that was not the question :) – transient_loop Aug 14 '20 at 18:57
  • @LeGEC 4th line gives this error: > git branch -u origin/staging warning: refname 'origin/staging' is ambiguous. fatal: Ambiguous object name: 'origin/staging'. – Shatiz Mar 16 '23 at 09:54
  • 1
    @Shatiz: this means you have two names that can match `origin/staging`. Check `git branch --format="%(refname)" -a | grep staging`, if you see `refs/heads/origin/staging` in that list it means you created a local branch named `origin/staging`. To delete that branch: `git branch -d origin/staging`. – LeGEC Mar 16 '23 at 10:05