Git branch management and manipulation

Original link: http://www.cnblogs.com/jf-67/p/7230949.html

  As we all know, using Git branch, we separated from the main line to develop, and continue working without affecting the main line.

  Since you want to use Git branch, here on the management and manipulation related to the Git branch, as listed branch, the branch is created, deleted branches, branch consolidation and other operations.

  This headache ever seen, always able to understand today studied a long time before they get to know, to be a note here.

  For example, my Git tool installed in the E disk> myGit during and after the installation directory is as follows:

  

  I created a local warehouse myProject, first of all we can first see the next branch when local warehouse. Git open tool operates as follows. 

jiangfeng @ jiangfeng MINGW64 ~ / Desktop
$ Cd e:

@ Jiangfeng MINGW64 Jiangfeng / E
$ Cd mygit

@ Jiangfeng MINGW64 Jiangfeng / E / mygit
$ Cd Go

@ Jiangfeng MINGW64 Jiangfeng / E / mygit / Git
$ cd myProject

  Check the local branch warehouses, command, git branch Enter to see the bifurcation in the warehouse, if not created, then by default only the main branch master:

@ Jiangfeng MINGW64 Jiangfeng / E / mygit / Git / the myProject (Master)
$ Git branch   see the bifurcation in the warehouse
 * master

  

  Next, create a branch, such as, I created a branch test in the warehouse and then wrote a test.txt file in the branch. So here it involves two operations, and that is to create a branch and switch. Specific operation is as follows: 

@ Jiangfeng MINGW64 Jiangfeng / E / mygit / Git / the myProject (Master)
$ Git branch test    branch name test is created

@ Jiangfeng MINGW64 Jiangfeng / E / mygit / Git / the myProject (Master)
$ Git checkout test    switch from the main branch master to test the newly created branch 
Switched Branch to ' test '

@ Jiangfeng MINGW64 Jiangfeng / E / mygit / Git / the myProject (Test)
$ Echo  " the Hello world! " > Test.txt   write a test.txt file in the branch test, the contents of the file as "hello world!"

  

  Here we want the file you just created is written to the cache. As follows:  

@ Jiangfeng MINGW64 Jiangfeng / E / mygit / Git / the myProject (Test)
. $ Git add    the file is written to the cache area, pay attention to spaces between the period and add 
warning: LF by CRLF by Will BE REPLACED in test.txt.
The file will have its original line endings in your working directory.

  

  To then add the contents of the cache file test.txt to the warehouse, as follows: 

@ Jiangfeng MINGW64 Jiangfeng / E / mygit / Git / the myProject (Test)
The commit git $ -m " Change log "   will add content to the buffer warehouse
[test afc4b17] change log
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt

 

  To merge branches below, about to test the new branch merged into the main branch master, there would first like to switch the test branch to the main branch master, and then merge the branch. As follows:

@ Jiangfeng MINGW64 Jiangfeng / E / mygit / Git / the myProject (Test)
$ Git checkout master   switches over into the main branch test Master 
Switched Branch to ' Master ' 
Your Branch IS up -to- DATE with ' Origin / Master ' .

@ Jiangfeng MINGW64 Jiangfeng / E / mygit / Git / the myProject (Master)
$ Git merge test   branch merge into the main branch test master
Updating 68d8bfc..afc4b17
Fast-forward
 test.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt

  

  If we want to submit the contents of the local repository to the remote GitHub, as follows: 

@ Jiangfeng MINGW64 Jiangfeng / E / mygit / Git / the myProject (Master)
$ Git push origin master   submit content to a remote GitHub 
Counting Objects: 3 , DONE .
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 282 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/leiwuhen-67/project.git
   68d8bfc..afc4b17  master -> master

 

  If you do not want to test the new branch with the main branch master merge, but would like to create a new file on the remote branch uploaded to the corresponding branch, then the operation is as follows. You should first establish local branches in the corresponding remote GitHub. As my local branch to the new test, then I on the remote GitHub new branch should also test, operate as follows:

  1. Open the Git tool, go to a local warehouse, as I was myProject, mainly because the default branch master, so first branch switching to the test, operating:  git Checkout the Test 

  2, on the remote test GitHub new branch corresponding to the local operator:  Git Push the --set-upstream test Origin 

  3, as I now test the new branch in a local test.txt file, the file content to operate as "the Hello world!":  Echo " ! The Hello world " > test.txt 

  4, add new files to the cache. Operation is:  git the Add. 

  5, add the buffer contents to a local warehouse, the operation is:  git the commit -m " test branch " 

  6, the contents of the local branch test submitted to a remote GitHub, the operation is:  git Origin test the Push 

  7, remote Delete Branch:  Git Push --delete Origin <BRANCHNAME> (or: git push --delete origin <branchName> ) 

  You need to perform Step 2 when you first create a remote branch of the future, if you want content on the local branch submitted directly to the corresponding remote branch git push on to: Note

 

  Last but not least, if you want to get the contents of the remote branch test to test the local branch, how does it work?

  In fact, very simple, open Git tool, go to the warehouse where the local branch, then git pull origin test can be, for example, my local warehouse in the E drive> myGit> Git, warehouse called myProject, then my operation as follows:

   E CD:  ,  CD mygit  ,  CD Git  ,  CD MyProject  ,  Git Origin pull test  (since I am here is to go directly to the branch test, if it is, will have to start switching to the main branch maste test branch, making this operation)

  Be here, then test the contents of the remote branch has acquired the local branch of the test.

  Similarly, if I want to speak the local branch of test content submitted to the remote branch master, then my operation was:  git Origin test the Push 

  

  to sum up:

  1, View branch: git branch

  2, create a branch: git branch branch name

  3, delete the branch: git branch -d branch name

  4, branch switching: git checkout new branch name

  5, merging branches: git merge the new branch name

  6, the establishment of remote branch: git push --set-upstream origin branch name

  7, the content obtaining remote branch test as to the local branch test: git pull origin test

  8, submitted to the local branch test content on a remote branch test: git push origin test

  9, delete remote branch: git push origin --delete <branchName> (or, git push --delete origin <branchName>)

  10. Check all branches situation (local and remote): git branch -a

  11, and switches to create a branch: git checkout -b branch name

  12, configure the user name and E-mail:

    git config --global user.name Username

    git config --global user.email mailbox

  ps: join the server to delete a branch, but by local git branch -a still can see, the situation can be updated with the following command branch. git fetch origin --prune

 

Reproduced in: https: //www.cnblogs.com/jf-67/p/7230949.html

Guess you like

Origin blog.csdn.net/weixin_30703911/article/details/94783280