Thursday, February 7, 2019

Keep Various Versions of Apps with 'Git Branch'

Keep Various Versions of Apps with 'Git Branch'

For some reasons, we need to keep different versions of the same app in the git repository. For example, a new function such as user authentication is added and the old version must be kept in the same time for future reference.

A git command ‘git branch’ can be used to satisfy the need. The default branch of the repository is master, we can create a new branch to preserve the original version (i.e. 01_basic). Then we can keep on developing new function in the master branch.

Create a new branch to preserve the original version.

List all branch name:

> git branch 
* master

Create a new branch ‘01_basic’:

> git branch 01_basic
> git branch
  01_basic
* master 

If we need to switch to the 01_basic branch, just:

> git checkout 01_basic
> git branch
* 01_basic
  master

*: the current branch

Other git operations:

> git branch -m 01_basic basic  // rename branch
> git branch -d 01_basic        // delete branch
> git checkout -b 02  // create and switch to branch 02    

Push all branches to the remote repository (i.e. github.com)

After several modifications of code in the ‘master’ branch, branch ‘01_basic’ and branch ‘master’ have had two different codebases. What if we need to push all branches to the remote repository (i.e. github.com)?

We can do it as:

> git push origin --all
Username for 'https://github.com': chaoyee
Password for 'https://chaoyee@github.com':
   :
To https://github.com/chaoyee/laravel_product.git
   151c1b9..5618f74  master -> master
 * [new branch]      01_basic -> 01_basic

The two branches shown on the webpage of github.com

branch on github.com

No comments:

Post a Comment