Chapter Two Assignment 2nd--Su Zhihua

Source: Internet
Author: User
Tags using git

2. Build a HelloWorld project for each person and practice basic commands like Git add/commit/push/pull/fetch/clone. Compare the differences between the old and new versions of the project.(1) Create a repository:

Start by creating a new HelloWorld folder on your local computer, then open git Bash and turn git init this directory into a repository that git can manage by command:

Then edit the helloworld.c file

(2) Set user name, mailbox, add, submit and view status, log

A. Setting up a user name and mailbox

B. Add helloworld.c to staging area and submit to local repository

C View status, log

Git log displays the commit log from the most recent to the farthest, where the commit ID is the version number and can be used to implement version fallback using git reset--hard commit ID

(3) Set up a remote repository on GitHub and add SSH KEY

Set up a remote repository

Generate SSH KEY

It is important to note that the mailbox is your GitHub account registered mailbox, or you will not be able to associate the local warehouse and the local warehouse, there are similar errors

Add SSH KEY to GitHub

(4) associate the warehouse and upload the local repository to the remote repository

At this point we look at the corresponding repositories on GitHub and find that they are already in sync.

Since the remote library is empty, and when the first push master branch is added, -u git will not only master push the local branch content to the remote new master branch, but also associate the local master branch and the remote master branch. You can simplify the command at a later push or pull .

(5) Fetch, pull, clone from remote library

To facilitate comparison between fetch and pull, first modify the Helloworld.c file in the local workspace as follows and submit it to the local repository

Use git log to view specific submissions

A. Use git fetch to get the latest version from remote to local

Here git branch-a is showing all the branches (it can be seen that the current branch is in master), and Git branch-r is only showing the remote branch

In the case of different branches using the git diff command, the difference is that git fetch does not automatically merge, so what do I want to do with merge?

With the git merge command, the merge branch, as expected, there should be a conflict between the two branches, when the merge will be an error, and then have to resolve the conflict before merging, you can have the following situation

What??? Sunglass (▔︹▔ㄟ) (╯▔︹▔) ╯

Already up-to-date description has been merged ah, but switch to the corresponding branch, see HELLOWORLD.C content is still not the same, this is why?

A GITK command is recommended, which shows the good visualization and historical information of the branch,

Give an example to better illustrate this situation, assuming that the commit history on the Branch1 branch is as follows

Then create a new branch branch2, and continue to submit four times:

At this point the head pointer of branch1 points to the D,BRANCH2 's head pointer to H. "Already up-to-date" means that the branch you want to merge is already the parent of your current branch, where D is the parent of H.

Merging BRANCH1 branches into BRANCH2 is not useful, as BRANCH2 does not change, and it is necessary to merge the Branch1 pointer to commit history into

Even if you use git reset--hard h to force a pointer to a BRANCH1 branch, point to H

Back to the problem, Remotes/origin/master is obviously the parent of master, so it cannot be merged into the master branch.

In GIT fetch Origin master:temp This command means: Download the master branch of the remote Origin repository to local and create a new branch temp

Obviously this is the case where you can merge the master branch onto the temp branch

The purpose of the git pull command is to retrieve updates from a branch of a remote host and merge with the local designated branch.

Git pull < remote host name > < remote branch name >:< local branch name >

For example, retrieving the master branch of the origin host, merging with the local master branch, needs to be written as follows.

Git pull Origin Master:master

If the remote branch is merged with the current branch, the part following the colon can be omitted.

Git pull Origin Master

The above command indicates that the Origin/master branch is retrieved and then merged with the current branch. Essentially, this is equivalent to doing git fetch first and then git merge.

c. You can clone a remote library to a local repository using git clone

d. Create a new file in the remote repository, then git fetch, git clone

New Test.txt file

git merge

Git pull

(6) Summary:

Throughout the process, we can see the power of Git as a distributed system, and remote warehouses and local warehouses are both independent and tightly connected, and the presence of branches makes it possible to work together in a methodical manner. We can also know that after the "git Fetch" command is completed, the downloaded files will not be merged into the current working directory immediately, and if you want to update the files downloaded from the remote branch to the working directory, a merge ("git merge") operation is required. The problem with "Git pull" is that it hides the details of the process and merges it automatically, which causes the local working directory to be updated by the remote branch without confirmation, and, once the code is problematic, it is difficult to find the wrong place, so git fetch+git merge is recommended.

Chapter Two Assignment 2nd--Su Zhihua

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.