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

Re: how to check for uncommitted/unstaged changes on remote side before pushing

From
Dennis Kaarsemaker <dennis@kaarsemaker.net>
Date
Nov 9, 2015, 08:25 UTC
Message-ID
<1447057500.5074.8.camel@kaarsemaker.net>
In-Reply-To
<20151108212320.GA18762@torres.zugschlus.de>
On zo, 2015-11-08 at 22:23 +0100, Marc Haber wrote:
Show 5 quoted lines
> Hi,
> 
> I am trying to abuse git as a code distribution channel and would
> like
> to be able to trigger redistribution just by git push.
[insert obligatory remark about git not being a deployment tool]
Show 5 quoted lines
> The idea is to push to a remote to the branch that is currently
> checked out followed by a git reset --hard in the post-receive hook.
> I
> have already figured out that I need to set receive.denyCurrentBranch
> to ignore to be able to push to the currently checked out branch.

You'll need a new enough git, so you can set it to updateInstead (and maybe use a push-to-checkout hook).

> I am also aware that it is a good idea to git pull before git push
> just in case there were local commits on the remote.

No, hooks should never pull, merge or do anything that could be interactive.

Show 9 quoted lines
> git reset --hard will unconditionally throw away local uncommitted
> changes. I would like to detect this situation on the remote and
> abort
> the receive progress. But my pre-receive hook does not work as
> intended. Here is my code:
>
> [snip code]
>
> What is going wrong here?

You mention a post-receive hook first, but have written a pre-receive hook. Not sure if that's what you intended (or even if that's what's going wrong).

> If my entire approach is wrong, what is the recommended way to 
> prevent a repository with unstaged or uncommitted changes from being 
> pushed to?

Push-to-checkout is a very simplistic way of deploying and while it works in simple cases, I'd not recommend it.

Two safer/saner approaches are:
- Have a separate non-bare repo, and make the post-receive hook in a
  bare repo trigger a fetch+reset in the non-bare one
- Use git archive and symlink trickery for even better deploys

Questions like this come up in #git all the time, so I wrote up a few more detailed recipes here, including working hooks and config for all three ways of deploying: http://git.seveas.net/simple-deployments-with-git.html

-- 
Dennis Kaarsemaker
www.kaarsemaker.net
Previous: Marc HaberNext: Marc Haber
Message 2 of 4 in “how to check for uncommitted/unstaged changes on remote side before pushing”
  1. Marc HaberNov 8, 2015
  2. Dennis KaarsemakerNov 9, 2015
  3. Marc HaberNov 9, 2015
  4. Dennis KaarsemakerNov 9, 2015

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.