Mahesh
Mahesh

Reputation: 129

How to push to a branch in different project

So i have this scenario. I have 2 projects, P1 and P2. Also consider these projects have branches for ex: P1B1 and P2B1 (Project & Branch). I have want to push my local changes from P1B1 directly to P2B1 (Else creating a new branch and pushing to P2B2 is also fine. Please let me know if there is a simple way.

Upvotes: 2

Views: 4770

Answers (2)

Code-Apprentice
Code-Apprentice

Reputation: 83517

In the following, I assume that what you call "projects" are copies of the same code base, both of which are under version control and potentially have different changes and work occurring on their own branches. Git calls these "repositories". You can connect repositories to each other by creating remotes.

If P1 and P2 are repositories on different servers and you have a local repository on your own machine, you can create remotes for both of them:

$ git remote add p1 <URL to P1>
$ git remote add p2 <URL to P2>

Now you can interact with either repository with most git commands. For example, you can fetch all of the branches from both repos:

$ git fetch p1
$ git fetch p2

You can also push your current work on branch b1 to both of them:

$ git checkout b1
$ git push p1 b1
$ git push p2 b1

For more details, see Working with Remotes in Pro Git.

Upvotes: 3

Info-Screen
Info-Screen

Reputation: 145

According to the git documentation on git push (https://git-scm.com/docs/git-push) you can specify the repository and branch for a push

so a git push <R2 url> <branch name> (e.g. git push ssh://[email protected]/path/to/P2.git/ B1

From the documentation:

repository: The "remote" repository that is destination of a push operation. This parameter can be either a URL (see the section GIT URLS below) or the name of a remote (see the section REMOTES below).

Upvotes: 1

Related Questions