1

This question shows how to push up to a commit to the master branch (eg all parent commits of the specific commit):

How can I pushing specific commit to a remote, and not the previous commits?

The answer is :

git push <remotename> <commit SHA>:<remotebranchname>

so you can use git push origin <commit SHA>:master

However, I would like to automatically push that commit to the matching remote branch.

I can't just use git push <remotename> "<commit SHA>:$(git branch)" because git branch returns the local branches, I would like to have the matching remote branch (eg for example you can map the local master branch to map to the remote prod branch). I would like to take that into account.

Community
  • 1
  • 1
edi9999
  • 19,701
  • 13
  • 88
  • 127
  • Your understanding of git seems conflicted to me. What you are trying to do, essentially pushing individual commits from a branch to the remote, will not work if some commits in the interim are missing. Instead, you can update the branch directly using `git push origin branchname`, or create a fresh one using `git push origin local_branch:new_remote_branch`. – Anshul Goyal Jun 09 '15 at 12:53
  • I clearly understand git. My use case is that I want to publish only up to a specific commit and not further (because I might change those commits in the future). I created a function that does basically `git push origin :master` , where commit sha is a parameter. However, I'm now not on the master branch, so I would like my function to automatically push to the right remote tracking branch – edi9999 Jun 09 '15 at 12:56

1 Answers1

0

Due to how Git works, a commit does not clearly belong to a branch. Branches are just pointers to commits that are located somewhere in the global history tree, so when you want to push a certain commit individually to a remote branch, that commit does not say anything about which branch you could possibly mean.

The only thing you can do it is to find branches where the commit is contained on, using git branch --contains. If that only returns one branch, then you’re in luck and everything is clear but the command may return multiple branches if the commit is reachable by multiple branches. For everything else, like the tracking branch, you would have to look at your repository’s configuration.

However, your workflow seems a bit weird to me. If you keep wanting to push an older commit instead of your current branch, then what you should really do is to make a new branch. Have one branch which matches the remote one which you then can just push; and have one other branch that’s ahead of that branch where your development happens. And whenever you want to update the remote, you update your local branch that matches the remote and then just push that branch.

So your history would look like this:

              prod               master
               ↓                   ↓
* -- * -- * -- * -- * -- * -- * -- *

And you could just push prod using git push origin prod and make that resolve whatever remote tracking branch actually is behind.

poke
  • 369,085
  • 72
  • 557
  • 602
  • > However, your workflow seems a bit weird to me. : In my case what I want is to be able to push only commits that I have looked twice, so that I'm sure I don't commit something that I shouldn't be commiting (unnecessary code, ...). That's why I'm sometimes comitting only up to one commit. – edi9999 Jun 09 '15 at 13:58
  • My question can be simplified a lot : git branch returns the current local branch. However, I would like to get the remote branch that tracks the current branch using the command line (eg not looking into ~/.git/config – edi9999 Jun 09 '15 at 14:03
  • Even if you want to push one commit at a time, use a branch to do it. It gets simpler that way, and also a lot more clear. – poke Jun 09 '15 at 14:43
  • I'm using tig so pushing one commit is just one keystroke, it's just that my binding doesn't work because the branch name is incorrect. Creating a branch in my use case would be overkill – edi9999 Jun 09 '15 at 19:58
  • I'm going to use http://stackoverflow.com/questions/171550/find-out-which-remote-branch-a-local-branch-is-tracking – edi9999 Jun 09 '15 at 20:05