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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With