1

Say I have a repo, let's call it origin. I clone it into another repo, working. I make some changes (and commit them locally). I now want to push them back to origin.

My understanding of this is that git push will say "no, origin is a working copy, this will overwrite it". I have previously used the option (in origin) to allow pushes to overwrite the current working copy, and thus allow pushes. Is there a way (with or without that), to allow pushes to add to the commit tree, but not update origin/master or mess with the working directory?

In effect, git push is the inverse of git pull (acquire updates and update the working directory); what is the inverse of git fetch (acquire updates)?

zebediah49
  • 7,467
  • 1
  • 33
  • 50

2 Answers2

2

I would rather consider git push the reverse of git fetch.

git pull is simply git fetch + git merge.

For pushing, the best practice is to use as a remote a bare repo, because if you are pushing a branch which is currently checked out by a remote non-bare repo, its working tree won't be updated, which means the push will be rejected by default.
As mentioned in "Git push only for bare repositories?":

It is perfectly OK to push to any other branch in a non-bare repository.

That might be closer to what you are looking for: simply push a branch which isn't one currently checked out by your remote (non-bare) repo.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Yeah, I was hoping to avoid having to `git checkout -b not_master` on my remote semi-bare repo to allow this to work, but I guess that's necessary. – zebediah49 May 06 '13 at 06:04
  • As a followup, I suppose that since it can't keep track of `local/master` or whatever, it's necessary to update `master` proper to avoid orphaning the new commits. – zebediah49 May 06 '13 at 06:16
1

My understanding of this is that git push will say "no, origin is a working copy, this will overwrite it".

That's wrong. git push is more like "Hi, I got these changes, would you like to take them?" and if there are changes in the remote that have not been fetched or pulled to your local repo, the push is rejected. It never makes a merge on the remote end - Git requires you to fetch/pull the changes, merge them in your local repo, and then push.

git push -f is another story - it overrides whatever there is in the remote that would cause the push to fail.

1615903
  • 32,635
  • 12
  • 70
  • 99