2-minute git tutorial

Source: Internet
Author: User
Tags how to use git

2-minute entry to git

Ordinary people: "lend me 1000 yuan ". Programmer: "Let's borrow 1024 yuan for a whole piece ".

Today is 1024, a holiday for our programmers. First of all, I wish you a happy holiday for programmers and programmers ~ Then, send out a holiday gift. That's right. It's just a two-minute entry to git. Of course, this is what I took the time to translate today. The original Article is here. If there is something wrong with the translation, please point it out.

Thanks to "Git in 5 minutes", I decided to write a shorter getting started tutorial. Of course, this is an extremely simple git tutorial. However, it is sufficient for independent developers in the early stage, and it builds the foundation for your growth.

For example, a high school student who needs to complete his or her first programming assignment and does not want to share code with others may benefit from this entry guide. (I think, especially for my son, he has already written a lot of code, but does not want to spend time learning version control systems. This is written specifically for him, and of course I want to help others .) It taught my son how to use git. Unlike subversion, git is easy for my son to use and does not require servers at all (as long as he regularly backs up on his hard drive .). In the 2-minute getting started tutorial, you don't have to worry about servers. This is perfect.

I reiterate that this git tutorial is only applicable to independent developers who have regular backup habits. 2 minutes of getting started with GIT and the backup policy, you can submit files with peace of mind. If necessary, you can see the changes to the files or restore them to earlier versions.

Why is this important? Well, the most annoying and time-consuming experience a developer has never found that a program can run normally, but it won't work after it is changed. In this scenario, you can easily see the changes and return to the previous version. Furthermore, returning to the previous version gives you more freedom to experiment with new methods-as you can try again.

When you have a chance, you should be clear about learning about staging, branching, pushing to a remote version library, and pulling from a remote version library. But here you will learn things that can be used for life.

Start using git

If you have not installed git, check the installation guide here.

When you use git for the first time, we assume that you are working in a main folder of your project. The first thing you need to do is initialize your folder to use git:

1 git init

 Tell git the file you want to add

Now you need to tell git which files it needs to care about. If you have n files, you can add them as follows:

1 git add <file1>  <file2> … <fileN>

Or if you want to add every file in the folder, you can do this:

1 git add .

PS: the vertex is part of the command, representing the current folder

Submit update

Next, we need to submit the update. If you want to submit one or more files at any time, execute the following command:

1 git commit <file1> <file2> … <fileN> -m “This is your commit message"

Alternatively, you can submit all the files that have been changed since the last submission:

1 git commit -a -m “This is your commit message for all changed files"

Make sure that your submission information contains sufficient description information so that you can identify the version you want to return.

View previous versions

Now we need an effective way to view the previous version. To view your submission information (This number indicates the version) together with the "hash" of each version, you can use the following command to display them in the output of a version number line:

1 git log --pretty=oneline

This command will provide you with output similar to the following to display each submitted hash and their submission information:

1 wanted to commit2 13bbf385e6d1f94c7f11a4cdfa2a7688dfdd84f8 3rd3 a1696f671fb90dc8ea34645a6f851d0ab0152fc2 2nd version4 179e59467039c7a7b81f676297415c8e018542a0 first version

PS: You can also use

1 git log

To get more detailed output. Each version has more lines. You can also use

1 git log --pretty=oneline -- <filename>

To view the update information of a file.

PS: there is a space behind two dashes.

Restore a previous version

In the end, to restore to an earlier version, you only need to use the first few characters of the hash (enough to distinguish it ):

1 git checkout 

For example:

1 git checkout 179e59467039 -- myfile

 

The file named myfile will be restored to the hash ing version of 179e59467039c7a7b81f676297415c8e018542a0 (in this case, it is the first submitted version of the file ).

View changes

If you do not check the changed content for the first time, you will not want to retrieve the historical version of a file. To view the differences between the current and previous versions of an object, refer to the hash of previous versions:

1 git diff 

You can also compare two previous versions:

1 git diff 

Another thing: (optional) It may take up more than one minute.

When you can get many benefits from the above features, you will find more useful things here. If you don't want it to disturb you, change the time.

Sometimes, you are not sure what file is changed. To find it, you can use:

1 git status

This will generate a list of files in the file state. For example, if a file is not "Git Add", it will be listed as not tracked: if it is a file you are concerned about, you should add it.

The reason why I want to put this command "optional" in this two-minute tutorial is that it is a bit stupid. Because it can list a large number of files that you don't care about. For example, if you program in Python, it will display the compiled. PyC file generated by python. Then you may want to do something.

To solve this problem, you need to create a file named. gitignore in your project directory. For example, if you use python2.x for programming, you may want to include (at least): *. PyC

PS:. gitignore recognition * wildcard.

If you want to hide the entire folder, you can add a slash after the file name. For example, if you use python3.x and the compiled file is put in a file named _ pycache __, you will want to add the following characters to. gitignore:

  __pycache__/

The above is all. Make sure that this tutorial is at hand.

This is all you need to know about git preparation, as long as you have the habit of backing up regularly on the hard drive. If you do not want to remember anything except the main commit command, you can save this tutorial as a bookmarklist. Then, you can submit and compare various versions and return to historical versions without any difficulty!

Remember, this tutorial is as simple as possible so that you can use git to do something useful. To obtain more powerful features such as Branching and staging and share your code remotely, go to "Git in five minutes ", if you have the opportunity, you can go to a more detailed git tutorial.

  

2-minute git tutorial

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.