ZANARDI Bruno

Software Developper

About
Resume

Git Blog

How To: Share with world

previous home

related commands:
fetch pull push remote

Now you know basics about git, it’s time to share your work with your team and/or the world.

Note:
This post will only talk about local / remote without branches. This subject will be discuss in a future post.

Showing remotes

If you have cloned your repository with git clone, you have one remote repository configured. In all cases, to see configured remotes, run:

$ git remote
origin

With -v option, the command will add urls that Git use for read (fetch) and write (push) to that remote:

$ git remote -v
origin http://remote/repository/url (fetch) origin http://remote/repository/url (push)

Adding remote repository

git clone implicitly add the origin remote for you. If you want to add another remote explicitly, use the remote add command:

$ git remote add <shortname> <url>
$ git remote -v
origin http://remote/repository/url (fetch) origin http://remote/repository/url (push) <shortname> <url> (fetch) <shortname> <url> (push)

Now, for all other remote commands, you will be able to use <shortname> instead of <url>.

Inspecting remote

It’s possible to see some informations about a particular remote. Over origin, the command will answer:

$ git remote show origin
* remote origin Fetch URL: <url> Push URL: <url> HEAD branch: master Remote branches: master tracked <branch> tracked Local branch configured for 'git pull': master merges with remote master Local ref configured for 'git push': master pushes to master (up to date)

Downloading world work

To get datas from your remote repository you can run:

$ git fetch <remote_name>

This command will download all remote datas you don’t have locally, but without merging them in your local branches.
Now you can merge or rebase/merge remotes datas into local.

There is a way to do it in one command:

$ git pull --rebase=preserve

Some explainations? Ok: pull command without options will do a fetch then a merge, and the --rebase=preserve will ensure you to keep a clean history in complicated merge case.
By default, git pull will do:

$ git fetch
$ git merge origin/master

If the remote branch hasn’t move between 2 pull, there will be no problems. But if it move, the merge will do a true merge with a merge commit and keep the remote branch in history and it’s not that we want. It’s here that the --rebase option will save us.
With the --rebase=preserve option, Git make a rebase before the merge, but will preserve your local branching merge.

There is a configuration variable you can set:

$ git config --global pull.rebase preserve

With this configuration, a simple git pull will do git pull --rebase=preserve.
You can found more informations about git pull --rebase here.

Publishing your work

To send your local work to the remote repository, the command to use is realy simple:

$ git push <remote_name> master

But sometimes, when you’re not up to date locally with remote branch, the push will fail:

$ git push origin master
To /tmp/remote ! [rejected] master -> origin/master (fetch first) error: failed to push some refs to '/tmp/remote' hint: Updates were rejected because the remote contains work hint: that you do not have locally. This is usually caused by hint: another repository pushing to the same ref. You may want hint: to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' hint: for details.

Do not use the tip unless you set the pull.rebase preserve configuration. If not, use git pull --rebase=preserve.
Then you can do the push again.

Remember that tags are not pushed automatically. To publish them, use:

$ git push --tags origin master

Renaming / Removing remotes

Like for adding, it’s pretty obvious:

$ git remote rename <old-remote-name> <new-remote-name>
$ git remote remove <remote-name>
previous top home

Reminder

fetch

Download datas from <remote>

$ git fetch <remote>

Download datas from all remotes

$ git fetch --all

pull

Download and merge datas from remote

$ git pull

Download and securely merge datas from remote

$ git pull --rebase=prevent

or with config:

$ git config --global pull.rebase prevent
$ git pull

push

Send local datas to remote

$ git push <remote> master

Send tags to remote

$ git push --tags <remote> master

remote

Check configured remote repositories

$ git remote

Check configured remote repositories with urls

$ git remote -v

Show informations about remote

$ git remote show <remote-name>

Add new remote repository

$ git remote add <shortname> <url>

Locally rename remote repository

$ git remote rename <old> <new>

Locally remove a remote repository

$ git remote remove <remote-name>
previous top home