Git two-minute guide | "Turn" by the program engineer

Source: Internet
Author: User
Tags using git


We've released a lot of git-related articles, a series of tutorials ("Pro git series"), and a variety of unique stunts (10 tips to get your git level up a little). But this two-minute git guide is the most streamlined. If you want to learn more about git-related articles, please start here.

The following is the body of the Git two-minute guide.

Inspired by the Git five-minute guide, I decided to go further and write a git tutorial that I can read in less time. Of course, it means the simplest git!. But it's enough for a novice individual developer, but it also gives you a starting point to go deeper.

Some examples of what can be learned from this guide may be that a high school student is working on his (or her) first project and does not need to share the code with others. (specifically my son, he has written a lot of code, but there is no time to learn a version control tool.) This guide was written for him, and of course I think others can use it. For him, the use of git is very meaningful. Unlike subversion, he does not need a server to make it easy to use Git (as long as he regularly backs up the hard drive, and he does, of course). Just in a two-minute guide, there's no time to get involved with the server, so this guide is especially appropriate for things like my son.

Again, the GIT approach mentioned in this article is appropriate only for those individuals who regularly backup their hard drives. Two minutes git guide plus a backup strategy, you can confidently submit the file and know if you need it, you can view the changes or restore the previous version.

Why is it so necessary to learn git? Well, for developers, the most annoying and time-consuming thing to find is that the program that worked before is not working now. In this case, it can be helpful to see the previous changes and fall back to the previous version. Again, being able to fall back to the previous state allows you to test the new method freely – doing the experiment is absolutely no problem because you can always start again.

If you have a chance, you should definitely learn about staging (staging) and branching (branching), as well as uploading (pushing) and fetching (pulling) to (from) the remote repository. But the next thing you'll learn is also useful. Note: You can use the file path as mentioned below for the file name.

Pre-use settings

If you haven't installed Git yet, check the installation guide on the Getting Started page. For the first time using git, we assume that you are working in a folder that is the home folder of your project. The first thing you need to do is initialize the folder for git using the following command.

Git init

Tell git which files you want to work with

Now you need to tell git what files you need to consider. If you have n files, you can use

git add <file1> <file2> ... <fileN>

To add them. Or if you want to add all the files under the directory, you can use

git Add. (a period is a part of the command that represents the current directory.) )

Commit Changes

Next, we need to commit the changes. Any time you want to submit a file or multiple file changes, run

git commit <file1> <file2> ... <fileN>-M "This is your commit message"

Or, submit all the files that have changed:

Git commit-a-M "This was your commit message for all changed files"

Make sure your submission contains enough descriptive information so that you can figure out which version you want to fall back to.

View history

Now you need a way to view the old version. In order to view the commit information and the hash value of the commit (representing a serial number of the version), you can use the following command to output it as one version per line

Git log–pretty=oneline

Its output looks like this, and each commit hash value is displayed along with its commit information.

Dbe28a0a1eba45d823d309cc3659069fc16297e3 4th version I wanted to commit

13bbf385e6d1f94c7f11a4cdfa2a7688dfdd84f8 3rd

A1696F671FB90DC8EA34645A6F851D0AB0152FC2 2nd Versio

179E59467039C7A7B81F676297415C8E018542A0 first version

Note that you can also use the

git log

To output a more verbose message, each version of the information takes up more than one line, and you can use

Git log–pretty=oneline-<filename>

To see the changes to a specific file. (Note the second – the trailing space!) )

Restore old version

To restore a previous version of the file, you only need to use the first few digits of the hash value (to ensure sufficient sensitivity):

git checkout

Like what

git checkout 179e59467039-myfile

will return the contents of my file named MyFile to the 179E59467039C7A7B81F676297415C8E018542A0 state (this is the first commit version of this file)

View Changes

Usually you don't go back to the old version without first looking at the file changes! To see the difference between the current version and the historical version of the file, you need to indicate the hash value of the historical version:

Git diff-

You can also compare the differences between the two historical versions:

Git diff-

One more thing--optional--may make this article a minute more

Perhaps you can benefit from just using the above features, and you will find that there is one more thing that is useful. If you don't want to see it now, don't look at it--find a chance to see it next time. Sometimes, you don't know which files have been changed. You can find them using the following command:

git status

This will generate a list of files and their status. For example, a file that has not been ' git add ' will be displayed as ' untracked '; If you want to track this file, you need to add it. In my two-minute tutorial, I took this command as an option because the command might be a bit awkward. Because it might list a lot of files that you don't care about. For example, if you are programming in the Python language, it lists the compiled files generated by Python. PYc. You certainly want to solve this problem.

To solve this problem, you need to create a file called '. Gitignore ' in the directory. For example, if you are using a Python 2.x project, you will want to include in this file (at least):

*.pyc

Note that the. Gitignore ' can understand * wildcard characters. If you want to hide a full directory, you need to add a slash after the folder name. For example, if you use Python 3.x programming, all the files will go to a folder called __pycache__, so you need to add them in. gitignore

__pycache__/

This is the tutorial! Just keep this tutorial handy.

These are the things you need to know to start using git, as long as you have the habit of regularly backing up your hard drive. If you don't want to remember anything other than submitting a command, simply save the tutorial in a bookmark, and you'll be able to submit it without hindrance, compare it, and fall back to the old version! Keep in mind that this tutorial is exactly a minimal tutorial, so you can do something useful with git. For the powerful features of branching, staging and sharing code with others through remote servers, be sure to look at the five-minute git tutorial and if you have the chance you should even see a longer tutorial!  



From for notes (Wiz)

Git two-minute guide | "Turn" by the program engineer

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.