38

git remote --v show remote info

origin  https://github.com/test/testing-iOS.git (fetch)
origin  https://github.com/test/testing-iOS.git (push)

It shows that both fetch and push are using the same remote URL.

Question:

When will (if ever) remote URL for fetch and push be different?

What commands can you use to change remote URL for fetch or push separately?

Boon
  • 37,606
  • 51
  • 186
  • 296

2 Answers2

36

Yes (using different remote), and that is why Git 2.5 introduces a new ref shorthand @{push}.
See "Viewing Unpushed Git Commits"

What commands can you use to change remote URL for fetch or push separately?

You need a separate remote:

git remote add myfork /url/for/my/fork
git config remote.pushdefault myfork

The GitHub blog post "Improved support for triangular workflows" illustrates the use of @{push}:

https://cloud.githubusercontent.com/assets/1319791/8943755/5dcdcae4-354a-11e5-9f82-915914fad4f7.png

See what commits you've added to your current branch since the last push:

git clone https://github.com/YOUR-USERNAME/atom
cd atom
git config remote.pushdefault origin
git config push.default current
  • remote.pushdefault specifies where to push (to which remote repo).
  • push.default specifies what to push (what refspec), when no refspec is explicitly given.
    current, in that latter case, means "push the current branch to update a branch with the same name on the receiving end."

The following branch will fetch from one url, push to another:

git remote add upstream https://github.com/atom/atom
git fetch upstream
git checkout -b whizbang upstream/master

(Here the whizbang branches tracks upstream/master, but pushes to origin/whizbang)

git log @{push}..

This uses the new @{push} notation, which denotes the current value of the remote-tracking branch that the current branch would be pushed to by git push, namely origin/whizbang.
You can also refer to the push destination of an arbitrary branch using the notation whizbang@{push}.

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • What's the difference between remote.pushdefault and push.default? – Boon Jul 31 '15 at 13:40
  • @Boon `remote.pushdefault` specifies **where** to push (to which remote repo). `push.default` specifies **what** to push (what refspec), when no refspec is explicitly given. `current`, in that latter case, means "push the current branch to update a branch with the same name on the receiving end." – VonC Jul 31 '15 at 13:42
  • Thanks - so in your example, I get that whizbang tracks upstream/master, but where does it specify that the push will go to origin/whizbang? – Boon Jul 31 '15 at 15:21
  • @Boon `git config remote.pushdefault origin` will make any branch push by default to `origin`. – VonC Jul 31 '15 at 15:22
  • Thanks @VonC. Why would it push to origin/whizbang when whizbang is set up to track upstream/master? Should it push it to upstream/master instead? – Boon Jul 31 '15 at 19:28
  • @Boon no: `git config remote.pushdefault origin` means any remote tracking branch will push by default to `origin` (that is the **where** I mention in the answer.). `git config push.default current` means it will push to a branch with the same name (that is the **what**) I mention in the answer. – VonC Jul 31 '15 at 19:29
10

From documentation, Fetch and Push URLs should be same.

http://git-scm.com/docs/git-remote

Command to set Push and fetch urls

'git remote set-url' [--push] <name> <newurl> [<oldurl>]

Note that the push URL and the fetch URL, even though they can be set differently, must still refer to the same place. What you pushed to the push URL should be what you would see if you immediately fetched from the fetch URL. If you are trying to fetch from one place (e.g. your upstream) and push to another (e.g. your publishing repository), use two separate remotes.

Suresh Sajja
  • 442
  • 3
  • 8