Setting up different fetch and push URLs in Git

1 min Edit
"There are two ways of exerting one's strength: one is pushing down, the other is pulling up."
Booker T. Washington,

One of the Git strategies that have worked well in my current project that I am working on is to have a separate fetch and push URL for a given repository.

What this allows you is to have a strategy where all developers pull the latest code from the main repository, however, before they contribute back to it, they need to do so by first pushing against a fork of the main repository. Once the developer is happy with his series of commits and is ready to merge back to the main repository, they create a pull request with the modified unit of work.

This process has been really helpful in ensuring that all code gets reviewed before it is merged in to the main repository.

Some Git clients like GitExtensions on Windows allow for a developer to manually set a custom push URL. Other clients such as Altassian’s SourceTree do not support this feature just yet.

So here is how you can set up a separate Git push URL through a command line:

Setting a separate Git push URL

Step 1: List your existing remotes in order to understand your current local setup.

$ git remote -v

# origin  [email protected]:PROJECT/REPOSITORY.git (fetch)
# origin  [email protected]:PROJECT/REPOSITORY.git (push)

Step 2: Use git remote set-url --push to modify the push URL only of your current repository. Using git remote set-url will modify both fetch and pull URL’s.

$ git remote set-url --push origin [email protected]:USERNAME/REPOSITORY

Step 3: Verify that the remote URL has been changed successfully.

$ git remote -v

# origin  [email protected]:PROJECT/REPOSITORY.git (fetch)
# origin  [email protected]:USERNAME/REPOSITORY (push)