A simple tutorial on git and GitHub

Source: Internet
Author: User

This article is only a part of the order, almost enough to the extent of the line. If you need another command, then you can go somewhere else to find out.
Write on GitHub first and then move in: This article GitHub link

Directory:

    • 0. What is git?
    • First, the main function of git: Version control
    • Ii. Overview
    • Third, Git for Windows software Installation
    • Iv. use of local git
    • V. GitHub's association with GIT
    • Vi. Other Introduction to GitHub
    • Vii. some problems that may be encountered

0. What is git?

I'm not telling you.

First, the main function of git: Version control
    1. Version:
      Think about your usual software, after the software upgrade, you use the new version of the software. You should have seen this version number: v2.0 or 1511 (for November 15 when publishing), such as:

      So if you modify and save a file, from the version management point of view, you get a new version of this file.
      But in many cases, this modification is irreversible. After you've modified it, you can't go back to the way you were before you modified it. To avoid this situation, some people will save the new version of the content into a new file.
      For example, in a folder called "Dissertation", he has the following documents:

      Just then, his girlfriend sent a copy 毕业论文_完成2.doc , this is a girlfriend to help him to change a paper. But has been through so many changes, and do not know what the other party changed, how to merge it? It seems that we have to start a heavy control and modify the work ...

      A more practical example is when programmers write programs:

      At this time you need a software to help you manage the version, it is git.
    2. Control:
      You can use Git to control these different versions. It is also easy to see the differences between the two different versions.
      --with Git, you can just save the latest file.
      --What about my previous documents?
      --You can use Git to help you back your files to the version you want.
      -If you go back, what about my latest version?
      --can also be restored.

Ii. Overview

All commands must be added before git , as indicated in the table init git init .
Click the command to jump directly to the place where this article was first used.
The following commands are executed at the command line.

1. Personal local use
Behavior Command Notes
Initialization Init Initialize the Git repository in the local current directory
Clone address Copy a warehouse (repository) from an address on the network to a local
View current status Status View the status of the current warehouse. If you don't know what to do when you encounter a problem, you can solve the problem by looking at the hints it gives.
View different Diff View the different places between the current status and the latest commit
Diff version number 1 version number 2 See the different places between the two specified versions. The version number here refers to the commit hash value.
Add File Add-a It's pretty generic. Add the first before commit
To withdraw something from the stage Checkout--. Here, the decimal point is used to recall all changes, and -- there are spaces before and after
Submit Commit-m "Submit Information" Submitting information best reflects what has changed
Delete an tracked Clean-xf Delete any files that have been track in the current directory. Whether it is the folder and the file specified in the. gitignore file
View Commit Record Log View the current version and previous commit record
Reflog change history of Head
Version fallback Reset--hard Version number Fallback to the version of the specified version number, after which modifications are removed. It is also back to the latest version through this command. Requires Reflog coordination
2. Personal use of remote repositories
Behavior Command Notes
Set User name Config--global user.name "Your user name"
Set up a mailbox Config--global user.email "your Mailbox"
Generate SSH Key Ssh-keygen-t rsa-c "Your Mailbox" No git in front of this command.
Add a remote repository Remote add origin you copy the address Set origin
Upload and specify the default Push-u Origin Master Specify Origin as the default host, after which the push is uploaded by default to Origin
Submit to Remote Repository Push Commits the added commit of the current branch to the remote repository
Synchronizing from a remote repository Pull Get a commit for a remote repository when the local version is lower than the remote repository version

You can visually see the impact of the above main commands on the warehouse using a single graph.

Image reference from: Git Introduction for CVS/SVN/TFS users


Images referenced from: Workspaces and Staging Area-Liaoche's official website (made a few changes)

View two pictures in contrast:

    • Workspace is the workspace, which is logically a local computer, not yet added to the repository state;
    • Staging is the stage in the repository, which is staging area. The modifications have been added to the repository, but have not yet been committed as commit, similar to caching;
    • The local repository is the master in the repository. This is the only step to success. Generate a new version;
    • Remote Repository is the repository. Used to upload a local repository to a network for backup, sharing, and collaboration. This article will use GitHub as an example of a remote repository.

Third, Git for Windows software Installation


Git for Windows (also Msysgit)

    • The installation package can be downloaded to the official website [1] or on GitHub [2] . If the download does not come down, you can copy the link to download with thunder. If you use Thunder download do not rest assured, after downloading to the place on GitHub download the SHA-256 value, and compared with the downloaded file, if the value of the same can be used with confidence.

    • Click on the line when installing Next .

    • Git for Windows starts with the 2.8.0 version [3] , adding environment variables by default, so the environment variables section is no longer manually configured. (This sentence can be ignored)

    • The window is small when you have just installed the open. If you're not used to it, you can make it bigger.
    1. First move to the bottom right edge of the window, and then pull the window up when the arrow appears.
    2. At the top of the window, click Icon, Options ...---Current size--OK
      This will then be resized after the window is opened.

Iv. use of local git

Instead of introducing GitHub, this is done on the local computer.

Open the command line (CMD) or right-click where you want to create the repository, and then tap Git Bash Here Open window.

1. New Warehouse-"Initialize"

Run git init to initialize the warehouse, such as:

It creates a hidden folder .git . It's for what it's for. Close the Windows 显示隐藏的项目 bar.

2. File additions and Submissions

I created a file in this folder today.txt . And not to wish myself a happy children's Day:

Then I use git status to see what's changed:

It tells me that there is a file that has not been traced, and that I can use git add <file>... it to add it in.
But I'm not going to introduce all the commands, so I chose to use the commands mentioned in the overview above git add -A .

Well, there's no hint. Never mind, we use again git status :

The state has changed! Description add succeeded. Take a look at its hints Changes to be committed , which means you can now execute a commit. The following line tells you how to move the file out of the stage, regardless.
Execution git commit -m "提交信息" submits the file to the repository. The submission information is enclosed in double quotation marks in English.

When you run, git log you can see the submitted record:

So the first step is done.

Perhaps you wonder: Why do you have to have an add, a direct commit is not OK? This is because the stage has a lot of uses, specifically to find relevant information. It's not a big problem here even if you don't know.

3. Modification of documents

Then I modify the contents of the file. Change to wish everyone happy children's Day Good (~ ̄▽ ̄) ~

Let's take git status a look at what's changed:

This is not the same as the previous hints. This was before:

A comparison will see that the previous add new file, when the file was not tracked (untracked), and this time is to change the files have been traced (tracked).

Now let's look at what changes the file has done through Git, and execute git diff :

It is compared by default with the latest commit.
Red (preceded by a minus sign - ) means delete, and green (preceded by a plus sign + ) indicates the addition.
So, in Git's view, we deleted the original line and added two new lines. This is more obvious when the contents of the file are particularly high.
This command can be used in the following situations:

    • You forget what you've changed, and you want to know.
    • Someone sends you a new version, you want to know what's changed

If I want to undo these changes now, I git checkout -- . 'll do the following:

Well, there's still no hint. Perform a git status look:

The previous status hint has disappeared. Take a look at the file:

It's restored! Then make the changes again:

Then:
git add -A
git commit -m "将[自己]改为[米娜桑]"

Take a git log look at the commit (commit) record:

Well. There are now two commits.

4. Version fallback!

Now I'm trying to fallback the file to the state of the first commit. But before that, let's look at the contents of this folder:

.gitfolder because it is hidden, I turned off 显示隐藏的项目 the option, it does not show.
Now you see only one file, and it's the latest version.

The modified date of the file is2016\6\1 21:52

From just now git log :

We see two lines of yellow in commit the beginning, followed by a string of characters. This string of characters is a 16 binary number, which is a string of hashes. We'll just call it the version number.

Start fallback, execute git reset --hard 1df0573 (take the first 7 digits of the version number to be able):

This indicates that head has been changed to point to 1df0573. At this time the file:

Its contents:

Has returned to my first version of the state.

The modified date of the file here is changed to my current time2016\6\2 19:29
This is because the modified date of the file was modified by Windows because it detected that the file was modified. And we just rolled back from the latest version to the current version, as if we had manually modified the contents of the file, which is actually done by git.
Actually, it doesn't matter whether the above

Now do it again git log :

The new version of the commit record is missing! This is the power of reset--hard, very good very tough!

So how do you get back to the latest version?
Execute first git reflog :

You can see the change of head.
The first line indicates that the current head is in the same version number, 1df0573 and the reason for this version number is that we executed the reset command.
Looking at the second line, it tells us that the version number of the head is the version ad93b89 number that was formed after the commit.

At this point I'll use reset again, point to head ad93b89 , and look at log:
git reset --hard ad93b89

Back to the first time before reset!

5. Clear files that are not tracked

There are usually two things to do before reset or pull (which can be said later):

    • Delete the newly added and tracked files (e.g., files generated after compiling the program)
    • The tracked files have been modified, but they are not required, and they are restored

Restore the modified tracked file, as already mentioned.
Now look at how to remove the newly added files with the command.

First I create a file manually to demonstrate:

With checkout is no way to remove it, using git clean -xf :

This command is more lethal, and it removes any files that have been track in the current directory. Regardless of whether it is the folder and file specified in the. gitignore file. Of course, there is also a relatively small damage, but here is not introduced.

V. GitHub's association with GIT

The above actions are all affected on the local computer, and generally enough.
If you're a programmer and want to share your code with others, or collaborate on development, you can use GitHub.

1) connection to local git and GitHub
    1. Register your [4] account with GitHub.

    2. Configure the user name and mailbox locally (skip this step if you've already set it up):
      • git config --global user.name "你的用户名"
        git config --global user.email "你的邮箱"

      • Or you change it directly in the config file, in the location C:\Users\你的用户名\.gitconfig . As shown, add the appropriate information:
    3. Generate SSH Key
      Run ssh-keygen -t rsa -C "你的邮箱" , it will have three times to wait for you to enter, directly enter can.

      Copy the generated SSH key to the Clipboard, and execute clip < ~/.ssh/id_rsa.pub (or go to the hint's path to open the file and copy it):
    4. Open GitHub and enter settings:

      Click on the left SSH and GPG keys and paste ssh key into the key on the right. Title can be named casually.

      Click on the following to Add SSH key add the success.
      Test it, do it ssh -T [email protected] :

      Well, that's the way it works!

2) Create a remote repository and associate with local
    1. Create a remote Warehouse
      First, in the top right-hand corner, click Enter to create the interface:

      Then enter the remote repository name:

      Click Create repository on it to create a good. Other options can be temporary.

    2. Associating a remote warehouse with a local warehouse

      Copy the SSH address of the remote repository to GitHub first:

      There are two ways to associate, one is SSH and one is HTTPS. Because HTTPS is slow, SSH is the recommended use.
      Note that the address format for SSH begins like this:[email protected]

      Run git remote add origin 你复制的地址 :

      Perform the git push -u origin master upload of the local repository to the GitHub repository and associate it with:

The above link has been completed!

Later you want to sync to GitHub after commit, just do git push it right:

You can see the changes on GitHub:

Vi. other GitHub introductions 1) Get other people's remote repositories

See other people's code, want to get to the local computer slowly study or modify, you can use git to download it down.
For example, I work with a classmate of the warehouse, Link: schaepher/blogsbackup

This is a small program that we use to back up blog Park blogs, mainly for teaching assistants to back up students ' blogs.

    1. Copy someone else's remote repository for SSH. method, like git above, can find the SSH address after entering the warehouse page.

    2. Performgit clone 复制的SSH地址

      The whole project 42M, took a little time to clone down.

One thing to note: This directly clone someone else's warehouse, cannot push on their own changes.

2) Another way to correlate remote empty warehouses

Once you know clone, you can create and associate an empty repository more easily.

    1. Create a warehouse on GitHub, as mentioned above, here is not much to say
    2. Copy the SSH for the warehouse you just created
    3. Clone to Local

So you don't have to do any more correlation. Omitted the above init and git remote add origin as well git push -u origin master .

3) Benefits of using GitHub
    • The world's largest co-♂ sex dating site (escape
    • (serious face) a lot of good programmers and the company's open source projects are placed on this, there is a wealth of resources to learn
    • Others (such as HR) can get a general idea of your level through your github. "How to spend two years interviewing a person" in the later part of this article is about GitHub
4) Team Work development

With regard to teamwork development, I have previously used a specific article to illustrate.
SOURCE Link: GitHub team project Collaboration process

The above directory:

    • 0. Pre-Preparation
    • First, create a development branch
    • Second, fork project to the personal warehouse
    • Iii. Clone Project to Local
    • Iv. keeping pace with team projects
    • V. Push changes to your project
    • Vi. request to merge on a team project
    • VII. team project leader Review and agree to merge request

零、一、七This is done by the team project leader.

Vii. some problems that may be encountered

This article basically only describes the main line operation, but in the course of operation, may be wrong operation or other problems. I put these questions in a separate blog (list here). This article will not appear too long.

      • I. Resolve conflicts that occur during merge
      • Ii. Fallback a merge
      • Iii. obtaining a modification of a commit
      • Iv. push the low version to GitHub (delete the high version commit)

Git and GitHub simple tutorials (GO)

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.