Disclaimer: As I am learning about git is still in the groping stage, some concepts of understanding may just I take out of context, there is a misunderstanding misleading place also please excuse me!
The concept of Branch 1.1 branching.
For the understanding of the branch, we can use the word modular to explain, in the daily work, a project development model is often modular, team-collaborative development. So the progress of our project can be called multi-core concurrency development. This modular development requires us to be as high-cohesion as possible, so as not to cause one arm to lose the whole person. Therefore, the concept of branching is introduced when all version controllers are managing the code. So what's the branch?
The branch is relative to the trunk, or is relative to the main branch, it is used to insulate the development of the feature. When we create a warehouse, the system creates the master branch by default, which is our default trunk branch. When we develop a project, after the framework has been built, we need to develop a module function, we will often create a branch to separate development, not with people in their own acres of three-point work, no impact on each other, When a function or module development is completed by testing the integration into the main frame, the advantage is that the development of a single module can avoid the shortcomings of the entire framework system cannot be compiled through, not functioning properly. If you are an Android developer, we can use the main thread (UI) thread and sub-threading to understand, the more similar is when we create an activity, the system will default to create a main thread, which is our UI thread, If we need to access the network to obtain data (time-consuming operation), our general practice is that we will re-open a sub-thread for remote data acquisition and resolution, when we complete the data read operation after the UI thread is updated, so that the time-consuming operation caused by the UI thread blocking (ANR).
1.2 Create branch and branch merge
Every time we execute commits, git will string them into a timeline, which is a branch of the line. After learning from the previous phase, we know that there is only one main branch in our warehouse, that is, there is only one time line, and as we commit each time, the master branch will think longer. When we needed to develop a new feature, we could create a new branch (DEV) to do the development of the module, and Git would create a new pointer (dev) and point the head to the Dev branch, indicating that the current branch is on Dev. Then the workspace modification and submission is on the Dev branch, and each time we commit, the dev pointer moves forward once, without affecting the master branch. After the development work on the Dev branch is completed, the Dev branch is merged with Master after the test is validated. So how do we merge? We can have the master branch point to the current commit of the Dev branch (which also points to the last commit under the current branch), which is to commit the dev branch as a modification of the master Trunk Branch, thus completing the merge. Once the merge is complete, we can even delete the merged dev branch.
Let's do the actual command operation:
1.2.1 Creating a Branch
$ git branch dev
! Note: Execute the above command without any hint!
1.2.2 Switching branches
$ git checkout devSwitched to branch ‘dev‘
! Attention:
1.2.3 Creating and Switching branch commands
Use the git checkout command plus the-b parameter to create and switch branches, equivalent to the two commands above;
$ git checkout -b dev
1.2.4 View Current Branch
Dev Master
The green section is the branch where the current branch is located.
[Email protected] ~/learngit (dev) $ git add test.txt[Email protected] ~/learngit (dev) $ git commit-m "Create new branch" [Dev CEE7BFC] Create new branch 1 file changed, 2 ins Ertions (+), 1 deletion (-)
[Email protected] ~/learngit (Dev)
$ git commit-m "Create new branch"
[Dev CEE7BFC] Create new branch
1 file changed, 2 insertions (+), 1 deletion (-)
The above code commits the modification under the Dev branch.
[Email protected] ~/learngit (dev) $ git checkout masterswitched to branch ' master ' Your branch was ahead of ' Origin/master ' by 1 commit. (use "Git push" to publish your local commits)
The above command is a branch switch that we have modified after the dev sub-section, when we look at the workspace, the changes under the Dev branch do not exist. If you want to see our changes under the Dev branch at master, we need to merge the branches.
$ git merge devupdating 94bf25d. Cee7bfcfast-forward Test.txt | 3 ++-1 file changed, 2 insertions (+), 1 deletion (-)
The execution of the above command completes the consolidation, at which point the files in the workspace are found, and the files under the main branch have been able to see the previous modifications under the Dev branch.
Attention! The "Fast-forward" in the prompt message printed after the above command tells us that this merge is "Fast forward mode", that is, to direct the master pointer to the current commit of Dev.
1.2.5 Deleting a branch
As we mentioned before, we can delete the merged branch after the branch merge, so let's delete the dev branch and see all the branches.
Master
The message tells us that the Dev branch has been deleted, the remaining branch is master, and the green indicates that the current branch is master
1.3 Resolving Branch Merge conflicts
You may have doubts in your mind when you see this topic? Didn't the above operation go well? How can there be conflict?
We look back at the operations of this branch merge and we find that when we merge the branches, the new branch Dev has changed, and the master branch has not committed any changes, but if we merge the branches, both the master and Dev branches commit the changes. Is it going to be smooth to merge branches? We're going to do the next thing with this problem.
We first create a branch dev, and then commit the changes on two branches respectively.
[Email protected] ~/learngit (master) $ git checkout-b dev2switched to a new branch ' dev2 ' [email protected] ~/learngit (DE V2) $ git add test.txt[email protected] ~/learngit (dev2) $ git commit-m "Create a new Brance dev2" [Dev2 046661c] Create a New Brance dev2 1 file changed, 2 insertions (+), 1 deletion (-) [email protected] ~/learngit (DEV2) $ git switch mastergit: ' Switch ' is not a git command. See ' Git--help '. [Email protected] ~/learngit (DEV2) $ git checkout masterswitched to branch ' master ' Your branch was ahead of ' Origin/master ' By 2 commits. (use "Git push" to publish your local commits) [Email protected] ~/learngit (master) $ git add test.txt[email protected] ~/learngit (master) $ git commit-m "add a new Lin E for Master "[Master 835e78c] Add a new line for Master 1 file changed, 2 insertions (+), 1 deletion (-)
Merge branches:
$ git merge dev2auto-merging test.txtconflict (content): merge conflict in test.txtautomatic merge failed; Fix conflicts and then commits the result.
Based on the above information, we find that the Test.txt file has a conflict and the merge failed. We can view the conflicting files according to the Git status.
$ git Statuson Branch Masteryour branch is ahead of ' Origin/master ' by 3 commits. (use "Git push" to publish your local commits) You have unmerged paths. (Fix conflicts and run "git commit") unmerged paths: (use "git add <file> ...." To mark resolution) both Modified:test.txtno changes added to Co Mmit (use "Git add" and/or "Git Commit-a")
Perhaps we can also view the contents of Test.txt directly:
<<<<<<< Headadd a new line for master. =======create a new branch dev2.>>>>>>> Dev2
Git uses <<<<<<,======,>>>>>> to mark the contents of different branches, we can modify the file as follows;
Add a new line for master. Create a new branch Dev2.
and submit in Master:
[Email protected] ~/learngit (master| Merging) $ git add test.txt[email protected] ~/learngit (master| Merging) $ git commit-m "fixed" [Master 51e165e] Fixed
The message tells us that the problem has been resolved. Then we can delete the Dev2 branch.
$ git branch-d dev2deleted branch Dev2 (was 046661c).
! Note: Our previous split-to-merge operations are performed in quick mode, but when you delete a branch in this mode, the branch information is lost. Therefore, we can also adopt the No-ff method when merging the branches, as below, interested friends can test themselves.
$ git nerge--no-ff-m "merge with No-ff" Dev
1.4 Hiding and resuming of branches
We can use Git stash to hide the current branch if we need to shelve the development of the current Branch and operate on the other part of the project during the development process.
$ git checkout-b dev3switched to a new branch ' dev3 ' [email protected] ~/learngit (DEV3) $ git add test.txt[email protected ] ~/learngit (dev3) $ git commit-m "use stash" [Dev3 D358fab] Use stash 1 file changed, 2 insertions (+), 1 deletion (-)
View status and execute the git stash command
$ git Statuson branch dev3changes not staged for commit: (use "git add <file> ..." To update what would be committed) (use "Git checkout--<file> ..." To discard changes working directory) MODIFIED:TEST.TXTNO changes ad Ded to commit (use "git add" and/or "Git commit-a") [email protected] ~/learngit (DEV3) $ git stashsaved working directory and index state, WIP on Dev3:d358fab, use Stashhead are now in D358fab use stash
Switch back to the main branch for the commit modification operation.
$ git Statuson Branch Masteryour branch is ahead of ' Origin/master ' by 6 commits. (use "Git push" to publish your local commits) Changes not staged for commit: (use "git add <file> ..." To update what would be committed) (use "Git checkout-&L T;file>, ... "to discard changes in working directory) Modified:test.txtno changes added to commit (use" git ad D "and/or" Git commit-a ") [email protected] ~/learngit (master) $ git add test.txt[email protected] ~/learngit (Master) $ gi T commit-m "Hello" [master 404a601] Hello 1 file changed, 2 insertions (+), 1 deletion (-)
Toggle Dev3 Branch
[Email protected] ~/learngit (master) $ git checkout dev3switched to branch ' Dev3 '
After switching back to the branch, we need to restore the scene at this point.
First look at the hidden scene.
[Email protected] ~/learngit (DEV3) $ git stash list[email protected]{0}: WIP on Dev3:d358fab use stash
There are two ways to recover the site using git stash apply [[email protected]{0}], but after the recovery stash content is not deleted, we need to manually execute git stash drop to delete.
The second performs Git stash pop, recovering the contents of the stash while deleting it.
$ git Stash popon branch dev3changes not staged for commit: (use "git add <file> ..." To update what would be Committ ED) (use "Git checkout--<file> ..." to discard changes in working directory) Modified:test.txtno changes Added to commit (use "git add" and/or "Git commit-a") Dropped refs/[email protected]{0} (6696a348f1e160fa3f234dff50eaad0d 59e4d264)
After the modification is completed under the Dev3 Branch, the merge branch operation is performed.
! Note: If we develop a branch that is ready to switch to the main branch for merging, we find that the changes under that branch are no longer needed, and we need to execute git branch-d [branch name] if we want to delete the branch.
1.5 push local branch to remote branch
In general, we will modify and commit on the local branch, then merge with the Trunk branch and delete the useless branch, so we just push the trunk branch when we push the remote branch.
$ GIT push-u origin masterusername for ' https://github.com ': Huangyabin001password for ' https://[email protected] ': Count ing objects:31, done. Delta compression using up to 4 threads.compressing objects:100% (19/19), done. Writing objects:100% (29/29), 2.23 KiB | 0 bytes/s, done. Total (Delta 9), reused 0 (Delta 0) to Https://github.com/huangyabin001/learngit.git 1cf2aaa. 7b69267 Master, Masterbranch master set up to track remote branch master from Origin.
Note If you enter GIT push Origin master, there is a problem:
$ GIT push origin master
Fatal:unable to access ' https://github.com/huangyabin001/learngit.git/': Empty
Reply from server
Second, custom Git2.1 client Configuration 2.1.1 Core.editor
git defaults to calling the value of your environment variable editor defined as a text editor, and if not defined, it calls VI to create and edit, and we can use Core.editor to change the default editor.
$ git config--global core.editor emacs
2.1.2 Help.autocorrect
This configuration only works in Git1.6.1 and above, and if you make a wrong command, it will show:
$git com tig: ' com ' is not a git-command. See ' Git--help '. Did you mean This?commit
Coloring of 2.2Git
Git can color the content that is output to your terminal so that you can quickly analyze it with an intuitive interface.
git automatically adds color to most of your output as you need it.
$git config--global color.ui true