Git Tutorial 6 - Branch and Merge - Remote

At previous Git Tutorial 5, we explain how to create new branch and merge the changes in new branch to master. All we've done is at local repo. We didn't do anything related to remote repo, neither branch creation or doing merge. I've said, I want to share my project and code with everyone who is working with me or is interested at it, espicically someone who really wants to take part in my project and is planing to contribute to it in the form of providing source code or fixing some bugs. In that case we must make our branch or mergence visible at remote repo. That's this episode is talking about.

In this tutorial I'm going to show you how to do the following:

  1. List out all of branches at remote repo
  2. Create a new branch at remote repo
  3. Fetch the new branch to local repo
  4. Merge your new branch to master at remote repo
  5. Delete your new branch at remote

Here we go...

 

1. List out all of branches at remote repo

We've learned how to list out all of branches at LOCAL REPO, as below

git branch

List out all of branches at REMOTE REPO is similar to it, just followed by an option -r or --remotes

git branch -r

In the below screen, I create a new branch at local and use branch command to show you branches at local and remote

figure 6.1

As the figure shown above, there are 2 branches (dev and master) at local and only one branch (master) at remote (where origin's linking). The branch existing at remote is in red.

 

2. Create a new branch at remote repo

Pushing is how you transfer commits from your local repo to a remote repo. Let's commit a change to dev before push to remote

figure 6.2

The new commit ID is 9fba3c2 in my example. Then push the branch dev which's containing one more new commit than master.

git push origin dev

 

figure 6.3

The push's done. It displays a new branch from dev at local to dev at remote.

Take a look at the remote site to check if the new branch's created and new commit (9fba3c2) is there.

figure 6.4

 

figure 6.5

The new commit's there, the change in element title is there. Everything goes well! 

 

3. Fetch the new branch to local repo

In this section I'm trying to play a role of my teammate who are interested at branch dev for project gittutorials. He wants to append some words to the text 'Dev' in element title. So he's going to use git fetch command to import my new commit from the remote repo into his local repo. 

git fetch origin dev:dev

See what happen in his screen

figure 6.6

The command git branch show out there are 2 branches at local and one of them is dev which's containing the new commit (9fba3c2).

 

4. Merge your new branch to master at remote repo

Actually you have 2 ways to merge your branch to master

Way 1: you merge your branch to master and push you master to master at remote. But before that, do remember to fetch the latest master branch. We have discussed any detail in the last episode of the tutorials.

Way 2: do merge new branch to master on remote site. How to do it is base on what your remote site is... Take bitbucket as example

figure 6.7

 

5. Delete you new branch at remote
You can delete your new branch at remote if you've merged it to master and don't need it anymore.

git push origin :dev

Just keep the local branch name empty before colon. That will delete the branch dev at remote (origin's pointing to).

figure 6.8

Please note that it didn't delete the branch dev at local.

 

posted @ 2015-11-24 16:01  壹加壹先生  阅读(207)  评论(0)    收藏  举报