I’ve cloned some git repository, and did “git branch abcd”, whcih switched me to a branch derived from origin/abcd. abcd is not the default origin branch.
Then, I have created a feature branch “my_feature” out of abcd.
I want to do “git merge origin/abcd” into my_feature in a script that would be applicable regardless of the name of the origin branch used (or at least applicable to easy cases when there are no complex branch structures described in some other answers about git).
How do I find what is the “closest”/”parent” origin branch off which the current branch was created?
This is hard to do well. In git, a branch is just an auto-advancing pointer to a commit, and a commit can have any number of branch names on it. Consider this case:
You checked out branch “master” at
c, and committedy1,y2, andy3. Your history thus looks likea b c y1 y2 y3. Meanwhile master has advanced todande, but someone created a feature branch and committedf1throughf4based onc. Git has no way to determine that your branch came frommasterinstead offeature, so at best you will have a choice of branches to merge.Were you to do this automatically you would have to apply a heuristic about picking the shortest branch, or the longest branch, or the one with the most/least commits, or something else like that. Naturally, since there are so many options, it’s not really a good choice for a
gitbuilt-in function. However, using git’s “plumbing” functions, you can write your own: