Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pull from one remote branch to another branch in git?

Tags:

git

github

For safety reason, my supervisor doesn't allow me to pull his work directly from the master branch. So I created one branch remotely named 'subbranch' and clone it to my local repository. How could I let 'subbranch' keep up to date with the master branch? I just know how to use the GUI to do it. How to do it with command line?

like image 641
hidemyname Avatar asked May 07 '15 17:05

hidemyname


People also ask

How do I pull one branch from a remote?

Just doing a git fetch remoteRepositoryName branchName (eg: git fetch origin my_local_branch) is enough. Fetch will be done and a new local branch will be created with the same name and tracking will be set to remote branch.

How do I switch from one remote branch to another?

In order to switch to a remote branch, make sure to fetch your remote branch with “git fetch” first. You can then switch to it by executing “git checkout” with the “-t” option and the name of the branch.

How do I pull changes from a remote branch to a local branch?

In order to fetch these changes from your remote, or in other words, download the changes to your local branch, you will perform a Git pull. Under the covers, a Git pull is actually a Git fetch followed by a Git merge. Git pull is just a shortcut to perform both of these actions in one step.

What is git pull remote branch?

The git pull command is used to fetch and download content from a remote repository and immediately update the local repository to match that content. Merging remote upstream changes into your local repository is a common task in Git-based collaboration work flows.


1 Answers

If you have not done it already clone the repository from master and then create a local branch in your clone, tracking to your upstream branch. For example

git clone https://github.com/rgulia/myclone
cd myclone
git checkout -b subbranch origin/subbranch 

When you need to work in your clone, make sure you are on your branch.

git branch  # your current branch is highlighted with a '*'

Work on your branch normally, by committing changes to your branch. Push changes to your remote branch.

git commit
git push origin subbranch 

From time to time you want to update with the changes from master

git fetch                  # get the changes from the remote repository. 
git merge master           # merge them into your branch
git push origin subbranch  # push your changes upstream

The git fetch applies to all branches, including master. The git merge creates a commit in your branch. Since you always work in your branch, you never commit nor push to master.

You might have to resolve conflicts. See this excellent tutorial. http://www.vogella.com/tutorials/Git/article.html#mergeconflict

I hope this helps.

like image 107
rgulia Avatar answered Oct 19 '22 02:10

rgulia