Git push - How To

As I've mentioned before, Git push takes some getting used to. Issuing the simple command "git push" may not give you what you expect.

I've kind of gotten used to using git fetch to pull into FETCH_HEAD, and then seeing if I want to merge FETCH_HEAD into master. Here's the analog when pushing.

First, let's assume we're pushing into a Linux repository from a Windows client. Our Linux repo has just two files initially:

Linux

@linux:/tmp/gitTest (master#) $ git commit -a -m "Initial commit"
[master (root-commit) 262073b] Initial commit
 2 files changed, 3 insertions(+), 0 deletions(-)
 create mode 100644 file1.txt
 create mode 100644 file2.txt
@linux:/tmp/gitTest (master) $ git branch -va
* master 262073b Initial commit


We clone the repo on Windows, and then add a file to the repo :

Windows

$ echo "File 3" > file3.txt

@Win /c/tmp/gitTest (master)
$ git add file3.txt

@Win /c/tmp/gitTest (master)
$ git commit -m "Add file3.txt"
[master 3e8fb2e] Add file3.txt
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 file3.txt

@Win: /c/tmp/gitTest (master)
$ git branch -va
* master                3e8fb2e [ahead 1] Add file3.txt
  remotes/origin/HEAD   -> origin/master
  remotes/origin/master 262073b Initial commit


Now for the push. The key here is to push into a NEW branch in the remote repository. Your push will CREATE this new branch. This technique was inspired from here :
http://blog.endpoint.com/2008/07/git-push-know-your-refspecs.html

Pay special attention to the format of what you're pushing into:

git push origin master:refs/heads/push-2009-07-28-1705

Note here "origin" could be any of your remotes. I just used origin because it's common.

Windows

@Win: /c/tmp/gitTest (master)
$ git push origin master:refs/heads/push-2009-07-28-1705
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 296 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://linux/tmp/gitTest
 * [new branch]      master -> push-2009-07-28-1705


Now go back to Linux and see what was pushed. If it looks good, merge that new branch into master. You can keep the newly pushed branch around for a while if you want.

Linux

@linux:/tmp/gitTest (master) $ git branch -va
* master               262073b Initial commit
  push-2009-07-28-1705 3e8fb2e Add file3.txt
@linux:/tmp/gitTest (master) $ git diff --stat master push-2009-07-28-1705
 file3.txt |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
@linux:/tmp/gitTest (master) $ git merge push-2009-07-28-1705
Updating 262073b..3e8fb2e
Fast forward
 file3.txt |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 file3.txt
@linux:/tmp/gitTest (master) $ git branch -va
* master               3e8fb2e Add file3.txt
  push-2009-07-28-1705 3e8fb2e Add file3.txt
Categories: