2

I have branches A and B in git. Here I am cherry picking changes from branch A to branch B. For this I have created a tag in branch A and made 10 different commits on top of it. The first commit has been tagged in branch A.

Git cherry pick command takes commit hashes as input.

For this I have a list of commit hashes (SHA) starting from a tag (in branch A) in a variable like this.

Checkout branch A

commithash="$(/usr/bin/git log --boundary --pretty=oneline ${tag_name}.. | sed -e 's/- //'| cut -d ' ' -f 1)"

echo $commithash  ---gives spaces delimited commit hashes below. 

3f899d3eebce23ce6a1efd3b73ea0b328c8cf3e1 2180e9e8a0a73a5313e70317a20360e3314b2568 f001ad3d03b6a4231d370a3b389d6edbd14db73b 66049c094a0780df7a518ba75d677213168e5211 3a6e53426e85f36b8fc3e19586c18d1ee0abf31b 0b2698bf67ea0c29f0e76fe110a0b89ccbfdecd2 8aed2c998dcd38ac5e01be5d1004d7130b1b2458 07a65386ad8883bf5f774541461cce0779d8b0d2 b57d0d1cea1cf8a151f1244c30b8f08eb7583926

I now switch to branch B

For each commit hash in that variable I want to run the command.

git cherry-pick --strategy-option theirs $(for-each-commit-sha)

When running the command it is successful except for a below scenario where it fails.

A. if the output for cherry pick command has the below output the script fails and none of the file are cherry picked.

error: could not apply 34h5432.. adding dev text for cc in file
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

Below is the output of:

git status 

On branch B
You are currently cherry-picking commit 34h5432...
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add/rm <file>..." as appropriate to mark resolution)

    deleted by us:   cc.txt

In this scenario I want to run git add cc.txt or issue a git add for whatever is mentioned after "deleted by us: "

git add cc.txt 

git commit -m "adding file" 

Then continue cherry pick for rest of the commit hashes in the list until the last.

The cherry pick needs to be done from last commit sha to first. In this case start with b57d0d1cea1cf8a151f1244c30b8f08eb7583926 and ends with 3f899d3eebce23ce6a1efd3b73ea0b328c8cf3e1

How can I do that ?

commithash="$(/usr/bin/git log --boundary --pretty=oneline ${tag_name}.. | sed -e 's/- //'| cut -d ' ' -f 1)"
Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
Spartan87
  • 119
  • 1
  • 8
  • Hi Spartan. I've tried to reformat a bit but was unsure about the title. Probably could be clearer, if I may. Try to formulate it as an actual question if you can. – Romain Valeri May 29 '19 at 01:10
  • @RomainValeri Thanks! I have all the commit SHA's in the Branch A and I need to recursively cherry pick all the commits listed in commithash variable to branch B. When cherry picking I want to handle the scenario mentioned above. – Spartan87 May 29 '19 at 01:31

1 Answers1

0

First, git cherry-pick can take a range of commits: you shouldn't have to list those commits manually.

Second, see if git checkout --theirs -- cc.txt would restore your file, before doing an add and git cherry-pick --continue.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I can supply the list of commits SHA at a time for the cherry pick command but I want a way to do this in a bash script. There is no "continue on error flag" for cherrypick. The script should cherry pick all sha's from other branch. On error grep for "Deleted by us:", add all the files listed in the grep using git add and then continue with cherry picking till the last commit. – Spartan87 May 30 '19 at 15:20
  • @Spartan87 "There is no "continue on error flag" for cherrypick": no, but if the cherry-pick returns anything else than 0, the commands did not complete: do then the checkout I propose in the answer, and `git cherry-pick --continue` until you see an exit status equals to 0 for that cherry-pick command: all that seems scriptable. – VonC May 30 '19 at 22:59