简体   繁体   中英

Git: find local branch used to push to origin

New to git.

I am in a situation where I have a bunch of local branches and I want to track down the one I was working on to push up changes to the origin.

What git command can I use?

I did git log origin/branch7 and that returned a whole bunch of commits.

in particular this one:

commit 5473e16761f4074a69321b671f88e6a66103c41f
Author: Me
Date:   Mon Jan 13 12:17:26 2014 +1100

    Encapsulate static inner class. Formatted code.

How can I use the commit ID to track down which local branch I used to push up those changes?

You can use:

git branch --contains <commit id>

in your case

git branch --contains 5473e16761f4074a69321b671f88e6a66103c41f

Did some reading here and ran:

git show-branch 

and that listed all the commits I made for all of my local branches. This enabled me to track down which local branch I was working on for that particular commit.

Ultimately there's no guaranteed answer to the question. The problem comes down to the fact that it's possible to push from any commit-ID, to any write-able name on any remote you can write on:

git push +1234567:origin/bluebranch

Assuming you have a commit whose ID can be abbreviated 1234567 , this connects to the remote named origin and asks it to force-update its idea of branch bluebranch to be commit 1234567 (well, whatever the full SHA-1 is for that local commit-ID). That commit need not be the tip of a branch at all, in your own repo; it just has to exist.

(The remote has the right to refuse an update, even with the + or --force flag. But that's a different thing entirely.)

The best you can do is look at which branches are either set to, or contain, the commit identified on the remote. Note that a branch name simply indicates which commit in a graph of commits should be consider to be the "tip" of that branch:

        D--E  <-- branch1
       /
A--B--C       <-- branch2
    \
     F        <-- branch3

Here, commit B is present on all three branches, but is the "tip" of branch2 even though it's fairly far back on branch1 and one step back on branch3 .

Using git branch --contains (as in Sergio Aristizábal's answer ) you can find that commit B is contained within all three branches. Looking at git show-ref you can see what the tips of all branches are and compare raw IDs, to see if the remote's idea of the branch matches your own. However, if someone else has added commits and you have picked those up in your repo, but not on your local branch references, they won't necessarily match.


The most general rule is that you should not care how some remote got into the state it is now in, you should only care about what you want to have happen to it next. You just run git fetch to synchronize with "the state it is now in", then poke around to see the state, and choose what to do with your local branches (if any) to synchronize them with the remote (if desired), perhaps transplanting (rebasing) old commits and/or adding new commits (if/as desired).

Of course, people being people, getting branch names right works wonders for aiding one's memory. :-)


Here's some useful tips for "getting branch names right":

  • If possible, use the same name in your local repo as on the remote repo.

    It's really confusing when your local branch is named cool-hack but the remote branch is named tonka as a development code name.

  • Use a name that means something to all the people who will work on it.

    Code name tonka is great if that actually means something to everyone. Otherwise it's just baffling. You might as well call it cool-hack .

  • Use git config to set push.default to simple or upstream : this means that running git push with no options will attempt to push the local branch, whatever its name is, to its default upstream. With simple , this will refuse unless that branch really does have the same name. That is, if local branch tonka is "tracking" origin/tonka , and the current branch you're on right now is tonka , then git push (with no other arguments) will ask origin to update its tonka to match yours. But if local branch cool-hack is tracking origin/tonka and you're on cool-hack , the push will fail unless you're using the upstream setting.

  • Therefore, try to use local tracking branches, so that you can just git push (and also git fetch followed by git rebase or git merge ) with no extra typing required. Moreover git branch -vv will tell you the name of the remote branch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM