git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: I want "fast forward my workdir to upstream if it's safe"

From
Jeff King <peff@peff.net>
Date
May 8, 2009, 02:30 UTC
Message-ID
<20090508023028.GA1218@coredump.intra.peff.net>
In-Reply-To
<86prekfv7z.fsf@blue.stonehenge.com>
On Thu, May 07, 2009 at 02:40:00PM -0700, Randal L. Schwartz wrote:
Show 5 quoted lines
> So, what I need is a command, likely an option to "git merge" that says "do
> everything that a git merge would do except abort if it would have been a
> merge commit".  In other words, abort if the workdir is dirty or is not a
> fast-forward update to the upstream.  Bonus if it exits non-zero if
> something went wrong.

Can you define more clearly what you want, because you are asking for conflicting things. "abort if it would have been a merge commit" is purely about fast forward. But it sounds like you also care about "would merge have succeeded". So I think you are asking for:

  1. There are no local commits on the branch.
and one of:
  2a. There are no local edits.
  2b. There are no local edits in the same files as those that are
      affected by any new commits from upstream.
  2c. Any local edits you have done would not cause a conflict if merged
      with what's in upstream.
And before I discuss those further, let me address:
> Please don't tell me "use these three commands in this script".
> I want a *command* I can tell people in #git.

by saying that I don't think there is currently a single command to cover both (1) and (2) (any of the (2) options). So we need to talk about "use these three commands in a script" for a moment to figure out what such a command _should_ do, and then we can talk about putting it into a single command (and presumably making that command part of the git distribution) that you can tell people about in #git.

Both (1) and (2) involve finding out who your upstream is. As of 1.6.3, this is easy to do as:

  upstream=`git for-each-ref --format='%(upstream)' `git symbolic-ref HEAD`
One you have that, (1) is easy:
  test -z "`git rev-list -1 $upstream..HEAD`"
(2a) is also pretty easy:
  git diff-files --quiet && git diff-index --quiet
(2b) is a bit harder, but do-able:
  git diff-tree --name-only HEAD $upstream | sort >them
  (git diff-files --name-only; git diff-index --name-only) | sort >us
  test -z "`comm -12 us them`"

(2c) is the trickiest (and of course, therefore probably the one you want ;) ). I'm not sure there is a simple way to do it short of hacking git-merge to actually try the merge and roll it back. Because you really have to deal not just with merging actual text file content but with custom merge drivers.

So what is it that you're looking for?
-Peff

PS I took your request at face value, but I actually think this question shows that the asker is not using git as it was intended. Unless you are very clueful (i.e., willing to sort out any mess caused by a failed merge), you should almost certainly be merging with a clean working tree. In other words, you should not be afraid of making a commit, and _then_ merging, as you might be in a non-distributed VCS.

Previous: Randal L. SchwartzNext: Andreas Ericsson
Message 4 of 12 in “I want "fast forward my workdir to upstream if it's safe"”
  1. Randal L. SchwartzMay 7, 2009
  2. Wincent ColaiutaMay 7, 2009
  3. Randal L. SchwartzMay 7, 2009
  4. Jeff KingMay 8, 2009
  5. Andreas EricssonMay 8, 2009
  6. Jeff KingMay 8, 2009
  7. Eyvind BernhardsenMay 8, 2009
  8. Randal L. SchwartzMay 8, 2009
  9. Junio C HamanoMay 8, 2009
  10. Jakub NarebskiMay 8, 2009
  11. Eyvind BernhardsenMay 11, 2009
  12. Miles BaderMay 8, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.