5
\$\begingroup\$

The branches I need to merge are called test and test-passed. Merging will always be fast-forward, from test to test-passed as commits to test-passed are only done automatically from test. This is currently working, just wondering if the approach is correct. The script is executed by Hudson, once all testing is complete.

git status
git reset --hard
git pull origin test
git checkout origin/test
git pull origin test-passed
git checkout origin/test-passed
git merge origin/test
git push origin HEAD:test-passed

One specific question I have, is if I need to create local branches as well (-b) or is that not required?

Output from above:

+ git status
HEAD detached from origin/test-passed
nothing to commit, working directory clean
+ git reset --hard
HEAD is now at 16a2d8d updated version
+ git pull origin test
From ssh://github.com/myrepo.git
 * branch            test       -> FETCH_HEAD
Already up-to-date.
+ git checkout origin/test
HEAD is now at 16a2d8d... updated version
+ git pull origin test-passed
From ssh://github.com/myrepo.git
 * branch            test-passed -> FETCH_HEAD
Already up-to-date.
+ git checkout origin/test-passed
Previous HEAD position was 16a2d8d... updated version
HEAD is now at 2aa260d... Merge branch 'dev-integration' into test
+ git merge origin/test
Updating 2aa260d..16a2d8d
Fast-forward
 app/application.properties                                     | 8 ++++----           
 4 files changed, 14 insertions(+), 5 deletions(-)
+ git push origin HEAD:test-passed
To ssh://[email protected]/myrepo.git
   2aa260d..16a2d8d  HEAD -> test-passed
\$\endgroup\$
6
  • \$\begingroup\$ What branch are you on when you run the script? \$\endgroup\$ Commented Feb 24, 2015 at 15:08
  • \$\begingroup\$ Using the hudson git plugin, I checkout test, updated with output \$\endgroup\$
    – latusaki
    Commented Feb 24, 2015 at 15:11
  • \$\begingroup\$ So you want to make origin/test-passed identical to origin/test? Why bother having a local test-passed branch at all? \$\endgroup\$ Commented Feb 24, 2015 at 19:00
  • \$\begingroup\$ When test-passed is updated (meaning test has a stable build) a different service picks up code and deploys. \$\endgroup\$
    – latusaki
    Commented Feb 24, 2015 at 22:50
  • \$\begingroup\$ Four argument versions of fetch and pull don't update local branch refs (you can see that in your output test -> FETCH_HEAD. What are you expecting those lines are doing for you? Why are you checking out origin/test before checking out origin/test-passed? Checking out remote refs doesn't leave you on a local branch (you might already realize this because your push line uses HEAD to make the push work) but I figured it was worth mentioning specifically just to be sure. If your merge is always a fast-forward then use --ff-only on the merge to have git ensure that too. \$\endgroup\$ Commented Feb 27, 2015 at 12:37

1 Answer 1

2
\$\begingroup\$

use variables

Pulling common elements into variables will make those things easier to follow and change later.

BRANCH_TEST="test"
BRANCH_PASSED="test-passed"

git status
git reset --hard
git pull origin $BRANCH_TEST
git checkout origin/$BRANCH_TEST
git pull origin $BRANCH_PASSED
git checkout origin/$BRANCH_PASSED
git merge origin/$BRANCH_TEST
git push origin HEAD:$BRANCH_PASSED

error checking

Is there any chance one of the steps will fail? You could check the return value of each step or just set -e to get bash to exit on any command having an error.

There is also bash strict mode which builds on set -e with some other things to make coding in the shell less surprising.

documentation

It would be a good idea to explain what the intent of the code is.

shebang

Presumably this is a bash script. It would be good to make it clear by putting

#!/bin/bash

as the first line.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.