Create your first Git Repository

Source: Internet
Author: User

Create your first Git Repository

Although Git is indeed selected as a version control tool by many important software, it is not only used for these important software; it can also manage your shopping list (if they are important to you, of course !) Your configuration file, Weekly Report or diary, project progress log, and even source code!

It is necessary to use Git. After all, you must have been mad at a backup file because it cannot identify the published information.

Git cannot help you unless you start using it, and now is the best time to start learning and using it. Or, in Git's words, "no other push can be as helpful as the origin HEAD" (the journey of a thousand miles begins with the meaning of a single foot ). I promise you will understand this soon.

Similar to recording

We often use the term "snapshot" to refer to images on the computer, because many people are full of feelings about the albums filled with photos of different times. This is useful, but I think Git is more like a recording.

Maybe you are not familiar with the traditional recording studio-mounted tape recorder. It includes several components: a rotating shaft that can be turned or reversed, a tape that stores sound waveforms, and recording sound waveforms on the tape with a pickup header, or the sound waveforms on the tape can be detected and played to the audience.

In addition to playing the tape forward, you can also reverse the tape back to the previous part, or fast forward and skip the subsequent part.

Imagine the recording of the band in the 1970s S. You can imagine that they practice songs over and over again until all parts are perfect and then recorded on the audio track. At first, you will record the drums, then the bass, then the guitar, and finally the lead singer. Every time you record, the recording studio staff will reverse the tape and enter the loop mode so that it will play the part you recorded previously. For example, if you are recording the bass, you will hear the drum in the background music, just as you are yourself playing the drum, and then the guitar player will hear the drum, the bass (and the ox bell) during the recording) and so on. In each loop, you will record a part. In the next loop, the staff will press the recording button to merge it into the tape.

You can also copy or replace the entire tape if you want to re-mix your work.

Now I want to give a vivid description of the recording work in the 1970s s, so that we can imagine Git's work as a recording job.

Create a Git Repository

First, we have to buy some tapes for our virtual recorders. In Git's words, these tapes are the repository; they are the basis for completing all the work, that is, the place where Git files are stored (that is, the Git workspace ).

Any directory can be a Git repository, but let's start with a new directory. The following three commands are required:

Create a directory (you can use your graphical file manager if you like .)
Switch to the directory on the terminal.
Initialize it into a Git-managed directory.

That is, run the following code:

$ Mkdir ~ /Jupiter # create a directory $ cd ~ /Jupiter # Enter the directory $ gitinit. # initialize your new Git Workspace

In this example, the folder jupiter is an empty but legal Git repository.

With the warehouse, we can proceed step by step. You can clone the repository. You can move back and forth between a historical point (provided that you have a historical point), create an alternate timeline, and do anything else Git can do.

Working in the Git repository is the same as working in any directory. You can create, copy, and save files in the repository. You can do everything as usual; Git is not complicated unless you think it is complicated.

In a local Git repository, a file can be in the following three States:

Untracked: You have created a file in the repository, but you have not added the file to Git management.
Tracked file Tracked: file added to Git management.
Staged: the modified tracked file and added it to the Git commit queue.

Any files you add to the Git repository are not tracked. These files are stored on your computer's hard disk, but you didn't tell Git that this is the file to be managed. We use our recorder for analogy, that is, the recorder has not been opened; the band started to work in the recording studio, but the recorder did not prepare the recording.

Don't worry, Git will tell you when this happens:

$echo"helloworld">foo$gitstatusOnbranchmasterUntrackedfiles:(use"gitadd<file>..."toincludeinwhatwillbecommitted)foonothingaddedbutuntrackedfilespresent(use"gitadd"totrack)

As you can see, Git will remind you how to add the file to the submit task.

Do not use Git commands for Git operations

To create a repository on GitHub or GitLab, just click a few times. This is not difficult. You can click "New Repository" and follow the prompts.

It is a good habit to include a "README" file in a repository so that people can know what your repository is doing when Browsing Your repository, what's more useful is to let you know what it has before cloning a warehouse with something.

It is usually easy to clone a repository, but obtaining the repository modification permission on GitHub is a little complicated. To verify through GitHub, you must have an SSH key. If you are using a Linux system, you can use the following command to generate:

$ssh-keygen

Then copy the content of your new key, which is a plain text file. You can open it in a text editor or run the following cat command to view it:

$cat~/.ssh/id_rsa.pub

Paste your key to the GitHub SSH configuration file or GitLab configuration file.

If you clone your project in SSH mode, you can write the changes back to your repository.

In addition, if Git is not installed on your system, you can use the file upload interface of GitHub to add files.

Tracking File

As the output of the command git status tells you, If you want git to track a file, you must use the command git add to add it to the submit task. This command saves the files in the temporary storage area. All the files are waiting for submission, or they can be used in snapshots. The purpose of the Git add command is different when you include files in snapshots and add new or temporary files to be managed by git, but at least for now, you don't have to worry about the differences between them.

This is like opening a recorder and preparing a recording. As you can imagine, you can press the pause button on the recorder that is already in the recording, or roll back the start to record the next audio track.

When you add a file to Git management, it identifies it as a tracked file:

$gitaddfoo$gitstatusOnbranchmasterChangestobecommitted:(use"gitresetHEAD<file>..."tounstage)newfile:foo

Adding a file to a submitted task is not "prepare a recording ". This only places the file in the prepare recording state. After you add a file, you can still modify the file. It is only marked as tracked and in the temporary storage area, so you can remove or modify a tape before it is written to it (you can also add it again to make some modifications ). But please note: You have not recorded the file in the tape, so if you break a file that is still good, you cannot recover it, because you didn't remember the file in the tape, it's still a good time.

If you finally decide not to record the file to the Git history list, you can cancel the submission task. In Git, you do this:

$gitresetHEADfoo

This actually removes the recording preparation status of the recorder. You just made a transfer in the recording studio.

Large submission

Sometimes you want to submit some content to the warehouse. In comparison to a recorder, it is like pressing the recording key and recording it into the tape.

In different stages of a project, you press the record key countless times. For example, if you try a new Python toolkit and finally implement the window rendering function, you must submit it, so that you can roll back to this stage when the new display options in the experiment are messed up. However, if you have drawn some sketch samples in Inkscape, you may have to wait until you have some content to develop before submitting. Although you may submit it many times, Git does not waste much or occupy too much disk space. Therefore, the more you submit, the better.

The commit command will "record" all the temporary storage area files in the repository. Git only records files that have been tracked, that is, all files that have been added to the temporary storage area by using the git add command at a certain point in the past, and files that have been changed since the last commit. If no submission was made before, all the tracked files will be included in this submission. From the Git perspective, this is a very important change because they have never been put into the repository and become put in.

To complete a submission, run the following command:

$gitcommit-m'Mygreatproject,firstcommit.'

This saves all submitted files and can then be used for other operations (or, in Gallifreyan, the Time Lord in the British TV series "Doctor of Mystery" said, they become "fixed time points "). This is not only a commit event, but also a reference pointer that you can find the commit in the Git log:

$ Gitlog -- oneline
55df4c2Mygreatproject, firstcommit.

To view more information, you only need to use the git log command without the -- oneline option.

In this example, the reference number is 55df4c2. It is called "commit hash" (LCTT Note: This is a hash code generated by the SHA-1 algorithm, used to represent a git commit object ), it represents all the new changes you submitted just now, and covers the previous records. If you want to "flip back" to your submission history point, you can use this hash as the basis.

You can think of this hash as an SMPTE time code on a sound tape, or a bit more. This is like the gap between two different songs on a black rubber record, or the audio track number on a CD.

After you modify the files and add them to the submission task, the submission is completed. This generates a new hash for submission, each of their marked historical points represents different versions of your product.

That's why musicians like Charlie Brown use Git as a version control system.

In the next article, we will discuss various aspects of Git HEAD, and we will truly reveal the secrets of time travel to you. Don't worry, you just need to continue reading (maybe you are already reading ?).

From: http:// OS .51cto.com/art/201608/515601.htm

Address: http://www.linuxprobe.com/first-git-warehouse.html


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.