0

As I was reading git: push a single commit and thinking about pushing commits in small batches, I started wondering if I could specify the local revision relative to the number of commits I want to push at once.

For example, let's say I have 12 outstanding commits to push. I can use HEAD~9 to push the first 3 outstanding commits; to push the next 3, I have to use HEAD~6 and so on. But I'd like to be able to use my shell's history without having to edit it for each batch.

$ git log --oneline myremote/mybranch..mybranch
0a594a4 Blah blah 12
9a4b42e Blah blah 11
d81227e Blah blah 10
803f1d3 Blah blah 9
aa7dcbc Blah blah 8
fc410e6 Blah blah 7
d57198e Blah blah 6
75f8ed6 Blah blah 5
4e801b4 Blah blah 4
7333461 Blah blah 3
3bc453e Blah blah 2
562690f Blah blah 1

Obviously I can push 1-3 with git push myremote 7333461:mybranch, I can also use HEAD~9:mybranch, but is there any way to say something like myremote/mybranch+3, to get the same hash? Everything I've tried with rev-parse takes me backwards, relative either to HEAD or myremote/mybranch (when it works at all).

Community
  • 1
  • 1
Wil Cooley
  • 900
  • 6
  • 18
  • possible duplicate of [How do I find the next commit in git?](http://stackoverflow.com/questions/2263674/how-do-i-find-the-next-commit-in-git) – Schwern Feb 16 '15 at 19:38
  • Hm... Possibly by specifying the src ref with command substitution in the shell but yuck -- I was hoping for something cleaner. (I'm not sure I'd call it a duplicate, either, since getting from your question to the answer I want is non-obvious and non-trivial.) – Wil Cooley Feb 16 '15 at 20:06
  • You're right, it only gets you about 80% there. You'd have to build your own alias. I retracted the vote to close. – Schwern Feb 16 '15 at 23:34

1 Answers1

2

The bad news is there cannot be an equivalent to HEAD^ that looks forward because the Git commit structure only knows about its parents. Sorry. You can write a utility to do it.

It has to have somewhere to look into the past from. You could accomplish this by walking back from each branch or tag until you find HEAD, but that could require a full tree traversal (not as inefficient as you'd think) and ambiguous.

However, your specific question has a crib. You can look backwards from the current branch's remote branch, which is available as @{u} ("u" for "upstream"). Put this program into your PATH as git-unpushed-ref or something.

#!/bin/sh

NUMBER=$1

git log --pretty=format:%H @{u}..HEAD | tail -n$NUMBER | head -n1

Now you can write git push origin $(git unpushed-ref 3):master or build a little shell wrapper around that.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • 1
    I think I misunderstood the intentions of your other question. If you are only interested in the branch you are currently on just nuke your tracking_branch function and replace `$remote_branch` with `@{u}` – Andrew C Feb 17 '15 at 01:14
  • @AndrewC Brilliant! Of course Git uses *yet another term* for the same thing so I can't find it in the docs. – Schwern Feb 17 '15 at 19:33