ABMagil
ABMagil

Reputation: 5595

How to Reference the Last Commit on Another Branch

I have two branches, brA and brB. Every change I make in brA needs to get cherry picked into brB. My desired workflow runs entirely from the command line and uses as few keystrokes as possible

Upvotes: 1

Views: 1133

Answers (2)

Alex Wolf
Alex Wolf

Reputation: 20118

A branch in git is nothing more but a reference on a commit; each commit has a reference on its parent commit(s) and this is how git builds the history.

When you make a commit on a branch then the reference points to the new commit which has the previous commit as its parent. This effectively means that a branch always points to the "latest" commit.

Obviously it's possible that you reset a branch and "undo" some commits this way, but now the reference points to a different commit so the other commits aren't part of this branch anymore.

This means that you just need to use the reference to get the "latest" commit of a branch which results in the following command:

git cherry-pick brA

On a sidenote: A workflow where you need to cherry-pick every and each commit from one branch onto another seems kinda off to me and there is probably a better way to achieve whatever you want to do.

EDIT I have to correct myself: Of course branches in git aren't symbolic references but just usual references; the only type of symbolic references in git can be found in HEAD. HEAD usually points at a branch and not directly at a commit as branches do.

Upvotes: 2

ABMagil
ABMagil

Reputation: 5595

Other branches' commits can be referenced with the @ notation as follows:

git cp brA@{0}

They count backwards from the last commit and are zero-indexed. I have no idea how it would handle a commit with two parents, but for now, that's not my problem. That's 11 non-varying characters, "git cp " and "@{0}. This gives me a keystroke total of:

13 + 8 + 11 + commit message + branch name

Upvotes: 3

Related Questions