Deep Learning: getting started with Git in Windows (below)

Source: Internet
Author: User

Disclaimer: as I am still in the exploratory stage of Git learning, my understanding of some concepts may be just out of context. Please forgive me for your misunderstanding and misinterpretation!

1. Concept of branch 1.1.

We can use the word modularization to explain the Branch's understanding. in daily work, the development mode of a project is often modular and team-based collaborative development. In this way, the progress of our project can be called multi-core concurrent development. This modular development requires us to have as high cohesion and low coupling as possible to avoid a situation where the entire arm is gone. Therefore, the concept of branch is introduced when all version controllers manage code. So what is the branch?

The Branch is relative to the trunk, or relative to the master branch. It is used to isolate feature development. When we create a warehouse, the system will create a master branch by default, that is, our default master branch. When we develop a project and the framework is built, when we need to develop functions of one module, we often create branches for separate development, no one is working in one acre or three plots of land, and there is no impact on each other. When a function or module is developed and tested, it is integrated into the main framework. The advantage of doing so is that, this prevents the development defects of a single module from causing the entire framework system to fail compilation and operation. If you are an Android developer, we can use the main thread (UI) thread and sub-thread to understand it. What is more similar is that when we create an Activity, the system will create a main thread by default, that is, our UI thread. If we need to access the network to obtain data (time-consuming operations ), in general, we will re-enable a sub-thread to obtain and parse remote data. After we complete the Data Reading operation, we will update the UI thread, to avoid the blocking of the UI thread (ANR) caused by time-consuming operations ).

1.2 create branch and branch merge

Each time we execute the commit, git will concatenate them into a timeline, which is a branch. After learning from the previous phase, we know that there is only one master branch in our warehouse, that is, there is only one timeline. With each commit, the longer the master branch takes. When we need to develop a new function, we can develop this function module when creating a new branch (dev, git creates a new pointer (dev) and points the HEAD to the dev branch, indicating that the current branch is on dev. The modification and submission of the workspace are on the dev branch. After each commit, the dev pointer will be moved forward once without affecting the master branch. After the development on the dev branch is completed, the dev Branch and the master are merged after the test and verification are passed. So how to merge? We can direct the master branch to the current commit of the dev branch (the dev branch points to the last commit under the current branch, that is, the dev branch is submitted as a modification of the master branch, so that the merge is completed. After merging, we can even delete the merged dev branch.

Next we will perform the actual command operations:

1.2.1 create Branch

$ git branch dev

! Note: No prompt is displayed when you execute the preceding command!

1.2.2 switch Branch

$ git checkout devSwitched to branch 'dev'

! Note:


1.2.3 create and switch branch commands

Add the-B parameter to the git checkout command to create and switch the branch, which is equivalent to the preceding two commands;

$ git checkout -b dev

1.2.4 view current Branch

$ git branch* dev  master
The green part is the branch of the current shard.

STAR@STAR-PC ~/learngit (dev)$ git add test.txt
STAR@STAR-PC ~/learngit (dev)$ git commit -m "create new branch"[dev cee7bfc] create new branch 1 file changed, 2 insertions(+), 1 deletion(-)

STAR@STAR-PC ~/learngit (dev)
$ git commit -m "create new branch"
[dev cee7bfc] create new branch
 1 file changed, 2 insertions(+), 1 deletion(-)
The above code submits modifications under dev points.

STAR@STAR-PC ~/learngit (dev)$ git checkout masterSwitched to branch 'master'Your branch is ahead of 'origin/master' by 1 commit.  (use "git push" to publish your local commits)
The above command is the branch switch operation after the next modification in dev. Now we can view the workspace, and the modifications under dev do not exist. If you want to see the modifications under the dev branch on the master node, You 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 preceding commands completes the integration. Now, you can view the files in the workspace and find that the files under the main branch can see the previous modifications under the dev branch.

Note! "Fast-forward" in the prompt message printed after the above command execution tells us that the merge is "Fast forward mode", that is, directly directing the master pointer to the current commit of dev.

1.2.5 Delete Branch

As we mentioned earlier, after branch merge, we can delete merged branches. Therefore, we can delete dev branches and view all branches.

STAR@STAR-PC ~/learngit (master)$  git branch -d devDeleted branch dev (was cee7bfc).STAR@STAR-PC ~/learngit (master)$ git branch* master
The prompt tells us that the dev Branch has been deleted, and the remaining branch is master. Green indicates that the current branch is master.
1.3 resolve branch merge conflicts

You may be confused when you see this question? Didn't the above operations go well? How can there be conflicts?

Let's look back at the above branch merge operations and we will find that when we merge the branches, the new branch dev has been changed, and the master branch has not submitted any modifications, but what if the master and dev branches submit modifications when we merge the branches? Will the merge branch work smoothly? We will proceed with this problem.

We first create a branch dev, and then submit the modifications on the two branches respectively.

STAR@STAR-PC ~/learngit (master)$ git checkout -b dev2Switched to a new branch 'dev2'STAR@STAR-PC ~/learngit (dev2)$ git add test.txtSTAR@STAR-PC ~/learngit (dev2)$ git commit -m "create a new brance dev2"[dev2 046661c] create a new brance dev2 1 file changed, 2 insertions(+), 1 deletion(-)STAR@STAR-PC ~/learngit (dev2)$ git switch mastergit: 'switch' is not a git command. See 'git --help'.STAR@STAR-PC ~/learngit (dev2)$ git checkout masterSwitched to branch 'master'Your branch is ahead of 'origin/master' by 2 commits.  (use "git push" to publish your local commits)STAR@STAR-PC ~/learngit (master)$ git add test.txtSTAR@STAR-PC ~/learngit (master)$ git commit -m "add a new line for master"[master 835e78c] add a new line for master 1 file changed, 2 insertions(+), 1 deletion(-)
Merge Branch:
$ git merge dev2Auto-merging test.txtCONFLICT (content): Merge conflict in test.txtAutomatic merge failed; fix conflicts and then commit the result.
When the preceding prompt is displayed, we find that the test.txt file is in conflict and the merging fails. You can view conflicting files based on 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 commit (use "git add" and/or "git commit -a")
Alternatively, we can directly parse the content of test.txt:

<<<<<<< HEADadd a new line for master。=======create a new branch dev2.>>>>>>> dev2

Git uses <, =====, >>>>> to mark the content of different branches. We can modify the file as follows;

add a new line for master。create a new branch dev2.
And submit in the master:
STAR@STAR-PC ~/learngit (master|MERGING)$ git add test.txtSTAR@STAR-PC ~/learngit (master|MERGING)$ git commit -m "fixed"[master 51e165e] fixed
The prompt tells us that the problem has been solved. Then we can delete the dev2 branch.
$ git branch -d dev2Deleted branch dev2 (was 046661c).
! Note: The split-to-merge operations are performed in quick mode. However, if you delete a branch in this mode, the branch information is lost. Therefore, when merging branches, we can also use the no-ff method, as shown below. If you are interested, you can perform the test on your own.
$ git nerge --no-ff -m "merge with no-ff" dev
1.4 branch hiding and Restoration

If we need to temporarily release the development of the current branch and perform operations under other points during project development, we can use git stash to hide the current branch.


$ git checkout -b dev3Switched to a new branch 'dev3'STAR@STAR-PC ~/learngit (dev3)$ git add test.txtSTAR@STAR-PC ~/learngit (dev3)$ git commit -m "use stash"[dev3 d358fab] use stash 1 file changed, 2 insertions(+), 1 deletion(-)
View the status and execute the git stash command
$ git statusOn branch dev3Changes not staged for commit:  (use "git add <file>..." to update what will be committed)  (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")STAR@STAR-PC ~/learngit (dev3)$ git stashSaved working directory and index state WIP on dev3: d358fab use stashHEAD is now at d358fab use stash

Switch back to the main branch to submit the 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 will be committed)  (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")STAR@STAR-PC ~/learngit (master)$ git add test.txtSTAR@STAR-PC ~/learngit (master)$ git commit -m "hello"[master 404a601] hello 1 file changed, 2 insertions(+), 1 deletion(-)

Switch to dev3 Branch

STAR@STAR-PC ~/learngit (master)$ git checkout dev3Switched to branch 'dev3'

After switching back to the branch, we need to restore the site.

First, view the hidden scenes.


STAR@STAR-PC ~/learngit (dev3)$ git stash liststash@{0}: WIP on dev3: d358fab use stash

One of the two methods to restore the site is to use git stash apply [stash @ {0}], but the stash content will not be deleted after recovery, we need to manually execute git stash drop to delete it.

The second method is to execute git stash pop to restore and delete stash content.


$ git stash popOn branch dev3Changes not staged for commit:  (use "git add <file>..." to update what will be committed)  (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/stash@{0} (6696a348f1e160fa3f234dff50eaad0d59e4d264)

After the modifications are completed under the dev3 branch, merge the branches.


! NOTE: If we have developed a branch and are ready to switch to the master branch for merging, we will find that the modifications under the Branch are no longer needed, at this time, if we want to delete this branch, We need to execute git branch-D [branch name]

1.5 push local branches to remote branches

Generally, we modify and submit the local branches, merge them with the trunk Branches, and delete useless branches. Therefore, we only need to push the trunk Branches to the remote branches.


$ git push -u origin masterUsername for 'https://github.com': huangyabin001Password for 'https://huangyabin001@github.com':Counting 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 29 (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, the following error occurs:

$ Git push origin master
Fatal: unable to access 'https: // github.com/huangyabin001/learngit.git/': Empty

Reply from server

Ii. Custom Git2.1 client configuration 2.1.1 core. editor

By default, Git calls the value defined by your environment variable editor as a text editor. If no definition is available, it calls vi to create and edit the editor. We can use core. editor to change the default editor.

$ Git config -- global core. editor emacs

2.1.2 help. autocorrect

This configuration is only valid for Git1.6.1 and later versions. If you mistakenly run a command, it will display:

$ Git com tig: 'com' is not a git-command.See 'git -- help'. Did you mean this? Commit

2.2Git coloring

Git can color the content output to your terminal so that you can perform quick analysis on an intuitive interface.

Git automatically adds color to most output as needed

$ Git config -- global color. ui true


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.