Git--xuexi xin de ti hui (TODO..)

Source: Internet
Author: User
Tags debian server using git

This is a bit long and messy, but like Mark. Twain blaise Pascal's joke says: I don't have time to make it shorter. There's a lot of discussion about this article in the GIT mailing list, and I'll try to put the relevant ideas down here.

One of the most common experiences I've talked about with Git is:

do not use git pull, git fetch and git merge instead. The problem with the

Git pull is that it hides the details of the process so that you don't have to understand the different types of branches in git and how to use them. Of course, most of the time this is fine, but once the code has a problem, it's hard to find the wrong place. It looks like the use of Git pull will surprise you, and simply look at Git's usage documentation to convince you. Another drawback of

Putting download (fetch) and merge (merge) into one command is that your local working directory will be updated by the remote branch without confirmation. Of course, unless you turn off all security options, git pull does not cause irreparable damage in your local working directory, but most of the time we prefer to do it slowly and do not want to rework it again.

650) this.width=650; src= http://static.oschina.net/uploads/user/ 90/180342_50.jpg?t=1371619625000 "alt=" Andy "title=" Andy "class=" smallportrait "/>            andy
Translated 3 years ago

2 people top

top   translation good!            

Branch (Branches)

Before we say git pull, we need to clarify the concept of branching (branches). Many people describe what a branch is like by writing a line of code, such as:

  • To be precise, the concept of branching is not a line, but is similar to a direction-free graph in development

  • Branching is similar to a large, heavyweight collection of objects.

I think you should understand the concept of branching: it is used to tag specific code commits, each branch is identified by a sha1sum value, so the operation on the branch is lightweight-you change only the Sha1sum value.

650) this.width=650; "src=" http://static.oschina.net/uploads/user/90/180342_50.jpg?t=1371619625000 "alt=" Andy " title= "Andy" class= "Smallportrait"/> Andy
Translated over 3 years ago

1 Person top

Top translation of good Oh!

This definition may have unintended effects. For example, suppose you have two branches, "stable" and "New-idea", with their tops in versions E and F:

A-----C----E ("stable") B-----D-----F ("New-idea")

So submissions (commits) A, C and E belong to "stable", while a, B, D and F belong to "New-idea". If you then use the following command to merge "New-idea" into "stable":

git checkout Stable # change to work on the branch ' stable ' git merge New-idea # merge in "New-idea"

... Then you'll get this:

A-----C----E----G ("stable") \/B-----D-----F ("New-idea")

If you continue to submit in the "New Idea" and "stable" branches, you will get:

A-----C----E----G---H ("stable") \/B-----D-----F----I ("New-idea")

So now A, B, C, D, E, F, G and H belong to "stable", whereas A, B, D, F and I belong to "New-idea".

Of course, the branch does have some special properties--most importantly, if you work on a branch and create a new commit (commits), the top of the branch will advance to that commit (commits). That's what you want. When merging with git merge , you just specify the merged branch to merge into the current branch and the current progress of the current branch.

650) this.width=650; "src=" http://static.oschina.net/uploads/user/497/995742_50.jpg?t=1368144691000 "alt=" super0555 "title=" super0555 "class=" smallportrait "/> super0555
Translated over 3 years ago

1 Person top

Top translation of good Oh!

Another common scenario that suggests that the use of branching can be very helpful is assuming that you are working directly in the main branch of a project (known as the "major version"), and when you realize that what you are doing may be a bad idea, it is too late, and you would rather be working on a topic branch. If the submission diagram looks like this:

Last version from another repository | V M---N-----O----P---Q ("master")

Then you take your work separately from the following set of commands (which shows the state that was changed after they were executed):

  git branch dubious-experiment  m---N-----O----P---q  ("Master"  and   "Dubious-experiment")   git checkout master  # Be careful  with this next command: make sure  "Git status"  is  #  Clean, you ' re definitely on  "master"  and the  #  " Dubious-experiment " branch has the commits you were working  #  on first...  git reset --hard <SHA1sum of commit N>         ("master")   m---N-------------O----P---q  (" Dubious-experiment ")   git pull # Or something that updates " Master " from           # somewhere else &NBSP;&NBSP;M--N----R---s  ("master")  &nbsP;           o---P---q  ("dubious-experiment") 

It's a look I've done a lot of things in the end.

650) this.width=650; "src=" http://static.oschina.net/uploads/user/525/1050041_50.jpg?t=1368272872000 "alt=" Zhao Liang-Blue Sky "title=" Zhao Liang-Blue Sky "class=" smallportrait "/> Zhao Liang-blue Sky
Translated over 3 years ago

1 Person top

Top translation of good Oh!

Branch Type

The term branching is not easy to understand, and there have been many changes in the GIT development process. But there are only two branches of git in its simplest way:

A) "Local branches", which is displayed when you enter "Git branch". For example, here's a small example:

$ git Branch Debian server * Master

b) "Remote Trace branch (remote-tracking branches)", when you enter "Git branch-r" is displayed, such as:


$ git branch-r cognac/master fruitfly/server origin/albert origin/ant origin/contrib Origin/cross-compile

As you can see from the above output, there is a "remote" tag name (e.g. origin, cognac, fruitfly) followed by a "/" in front of the name of the trace branch, and then the real name of the branch in the remote repository. ("Remote Name" is a code warehouse alias, and a local directory or URL is a meaning that you can freely define with the "Git Remote" command.) However, the "Git clone" command uses the name "origin" by default. )

650) this.width=650; "src=" http://static.oschina.net/uploads/user/90/180342_50.jpg?t=1371619625000 "alt=" Andy " title= "Andy" class= "Smallportrait"/> Andy
Translated over 3 years ago

2 Person top

Top translation of good Oh!

If you are interested in how the branch is stored locally, take a look at the following file:

  •  . git/refs/head/[Local branch]

  •  . git/refs/remotes/[ The branch being traced]

Two types of branches are very similar in some ways-they all just store a SHA1 checksum of the commit on-premises. (I emphasize "local" because many people see the "Origin/master" as saying that this branch is, in a sense, incomplete and does not have access to the remote server-this is not the case.
No matter how similar, they have a particularly significant difference:

  •   Changing the remote Trace Branch security method is to use Git Fetch or as a git-push by-product, you cannot do this directly to a remote trace branch. Instead, you have to switch to the local branch and create a new commit that moves to the top of the branch.

So, the most you can do with a remote trace branch is one of the following:

  •   using git fetch Update remote Trace branch

  •   Merge remote Trace branch to current branch

  •   Create a local branch based on a remote trace branch

650) this.width=650; "src=" http://static.oschina.net/uploads/user/524/1049534_50.jpg?t=1376472396000 "alt=" several people " Title= "Some people" class= "smallportrait"/> Several people
Translated over 3 years ago

0 Person Top

Top translation of good Oh!

Create local branch based on remote tracing branch

If you want to create a local branch (working on a local branch) based on a remote trace branch, you can use the following command: git branch–track or git checkout–track-b , Two commands allow you to switch to a newly created local branch. For example, you can use the git branch-r command to see a remote tracing branch with the name "origin/refactored" that you need, and you may have the following command:

      git checkout --track -b refactored origin/refactored 

In the above command, "refactored" Is the name of this new branch, and "Origin/refactored" is the name of the existing remote tracing branch. (in the latest version of Git, the '-track ' option in the example is no longer required, and if the last parameter is a remote trace branch, this parameter will be added by default.)

650) this.width=650; src= http://static.oschina.net/uploads/user/ 90/180342_50.jpg?t=1371619625000 "alt=" Andy "title=" Andy "class=" smallportrait "/>            andy
Translated 3 years ago

0 people top

top   translation good!            

The –track option sets a number of variables to maintain the dependencies of the local branch and the remote trace branch. They are useful for the following situations:

  • After the git pull command downloads the new remote trace branch, you can know which local branch to merge into

  • When you use git checkout to check your local branch, you can output some useful information:

Your Branch and the tracked remote branch ' origin/master ' have diverged, and respectively has 3 and 384 different C Ommit (s) each.

Or:

Your Branch is behind the tracked remote branch ' Origin/master ' by 3 commits, and can be fast-forwarded.

The configuration variables allowed are: "Branch.<local-branch-name>.merge" and "branch.<local-branch-name>.remote", but usually you don't have to consider their settings.

650) this.width=650; "src=" http://static.oschina.net/uploads/user/90/180342_50.jpg?t=1371619625000 "alt=" Andy " title= "Andy" class= "Smallportrait"/> Andy
Translated over 3 years ago

0 Person Top

Top translation of good Oh!

After creating a local branch from the remote Code warehouse, you will notice that "git branch-r" can list many remote tracing branches, but there is only one local branch on your computer, and you need to set a parameter to the command above to specify the correspondence between the local branch and the remote branch.

There are some terminology that are easy to confuse. Note that "track", when used as a parameter "-track", refers to a remote tracing branch that is corresponding to a local branch. In the remote tracing branch, it refers to the trace branch in the Remote Code warehouse. A bit around the mouth ...

Let's look at an example of how to update the local code from the remote branch and how to push the local branch to a new remote repository.

650) this.width=650; "src=" http://static.oschina.net/uploads/user/90/180342_50.jpg?t=1371619625000 "alt=" Andy " title= "Andy" class= "Smallportrait"/> Andy
Translated over 3 years ago

0 Person Top

Top translation of good Oh!

updating from a remote repository

If I want to update from the remote source repository to the local code warehouse, you can enter the command "git fetch Origin", which has the following input format:

  remote: Counting objects: 382, done.  remote: Compressing  objects: 100%  (203/203), done.  remote: total 278  (delta 177),  reused 103  (delta 59)   Receiving objects: 100%  (278/278),  4.89 mib | 539 kib/s, done.  resolving deltas: 100%  (177/177 ),  completed with 40 local objects.  from ssh://[email protected] /srv/git/fiji     3036acc. 9eb5e40  debian-release-20081030 -> origin/debian-release-20081030   *  [new branch]      debian-release-20081112 -> origin/ debian-release-20081112   * [new branch]       debian-release-20081112.1 -> origin/debian-release-20081112.1  &Nbsp;  3d619e7. 6260626  master     -> origin/master

The most important are these two lines:

3036acc.. 9eb5e40 debian-release-20081030-origin/debian-release-20081030 * [New branch] debian-release-20081112 origin/debian-release-20081112

The first line indicates that the commit (commit) ID of the remote origin/debian-release-20081030 branch has been updated from 3036ACC to 9eb5e40. The part before the arrow is the name of the remote branch. The second line is the action we take to create a remote trace branch (if a remote repository has a new tags,git fetch, it will also be downloaded locally).

650) this.width=650; "src=" http://static.oschina.net/uploads/user/90/180342_50.jpg?t=1371619625000 "alt=" Andy " title= "Andy" class= "Smallportrait"/> Andy
Translated over 3 years ago

0 Person Top

Top translation of good Oh!

The preceding lines show which files are downloaded locally by the git Fetch command, which, once downloaded locally, can be manipulated locally.

After the "git Fetch" command finishes, the downloaded files are not immediately merged into your current working directory, which gives you an opportunity to choose what to do next, and if you want to update the files downloaded from the remote branch to your working directory, you need to perform a "merge" operation. For example, my current local branch is "master" (after executing git checkout master), and I want to perform the merge operation:

git merge Origin/master

(A few digression: it is possible that you have not submitted any changes to the remote branch, or perhaps a complex merge.) )

650) this.width=650; "src=" http://static.oschina.net/uploads/user/90/180342_50.jpg?t=1371619625000 "alt=" Andy " title= "Andy" class= "Smallportrait"/> Andy
Translated over 3 years ago

1 Person top

Top translation of good Oh!

If you just want to see the difference between a local branch and a remote branch, you can use the following command:

git diff master Origin/master

It's a good idea to download and merge separately, and you can start by looking at what's being downloaded before deciding whether to merge with local code. and separate to do, you can clearly distinguish between the local branch and remote branch, convenient choice to use.

650) this.width=650; "src=" http://static.oschina.net/uploads/user/90/180342_50.jpg?t=1371619625000 "alt=" Andy " title= "Andy" class= "Smallportrait"/> Andy
Translated over 3 years ago

0 Person Top

Top translation of good Oh!

Push your changes to a remote repository

How does it go through the other way? Suppose you made a change to the "experimental" branch and wanted to push him to the "Origin" remote repository. You can do this:

?

1 git push origin experimental

You may receive: The remote repository could not fast-forward the error message for that branch, which would mean that someone else might have push a different change to that branch. So, you need to fetch and merge other people's changes and try the push operation again.

650) this.width=650; "src=" http://static.oschina.net/uploads/user/118/237609_50.jpg?t=1369142298000 "alt=" _ Raymond "title=" _raymond "class=" smallportrait "/> _raymond
Translated over 3 years ago

0 Person Top

Top translation of good Oh!

extended reading: If this branch corresponds to a different name (for example: Experiment-by-bob) in the remote repository, you should do so:
 git push origin  Experimental:experiment-by-bob 

In older versions of Git, if "experiment-by-bob" does not exist, the command should read:
        git push origin experimental:refs/heads/experiment-by-bob 

This will create the remote branch first. But git 1.6.1.2 should not do it. Take part in the comments below Sitaram ' s.
  If the local branch and remote branch name are the same, no special instructions are required the system will automatically create this branch, just like a regular git push operation.

in real-world applications, keeping the same name can reduce confusion, so "local name and remote name" as the "Refspec" parameter, we do not have much discussion.

git push operation does not involve the remote tracing branch ( Origin/experimental ) , only when you it will not be updated until the next git fetch.

This is wrong, according to Deskin Miller 's comments: When pushed to the corresponding remote branch, your remote tracking branch will be updated.


This article is from the "Mr_computer" blog, make sure to keep this source http://caochun.blog.51cto.com/4497308/1766941

Git--xuexi xin de ti hui (TODO..)

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.