0 Foundation can also learn the GitHub series of Git crash

Source: Internet
Author: User
Tags command line commit version control system git commands

1. What is git?

Git is a new Era version control system developed by Linux inventor Linus, what is a version control system? How to understand? A lot of detailed introductions on the web, but most of it is boring and difficult for beginners to understand, here I just give a few examples to help you understand.

Familiar with programming knowledge, we are in the software development of the source code is in fact the most important, then the source code management becomes extremely important:

For example, in order to prevent the loss of code, the local machine and remote servers are sure to store a copy, but also need a set of mechanisms to enable local and remote synchronization;

For example, we often do a few people to do the same project, have to make changes to a code, this time need everyone to not affect each other, but also need to synchronize the code of others;

For example, we have to develop a bug, sometimes just released the function of a serious bug, this time need urgent to restore the code;

For example, as our version of the iteration functions more and more, but we need to know clearly the history of each version of the code change records, and even know the history of each person to submit code;

And so on, such as the above, these are the version control system can solve the problem. So, versioning is a system that records changes in one or several file contents for future review of specific revisions, and versioning is the most important aspect of software development, and Git is no doubt the most popular and best version control system in the world.

  2. Git Installation

It says Git is a version control system, you can also be understood as a tool, similar to Java, you must first download the installation before use, so the first step must be installed, I use the Mac, Mac in fact, the system with Git, but here unified to provide the installation of each platform way, This part is not too much to introduce, I believe that everyone here to make a definite.

-Mac:[git-osx-installer DOWNLOAD]

-Windows:[git for Windows]

-Linux:

Debian series: Apt-get install git

Fedora on: Yum install Git-core

  3. How to learn Git?

After installing Git, how to learn is a problem, in fact, there are a lot of graphical software available for Git, but I strongly recommend that you start learning from the command line, and I know that people who haven't contacted the command line can be very inconsistent, but my hands-on practice proves that you only learn the command line at first, and then you Every step of the way to understand its meaning, and when you are proficient you want to use any graphical software to operate completely no problem.

I started by teaching our team members all based on the command line, and later proved that they are now deeply in love with the command line can not extricate themselves, they understand the specific meaning of Git every step, so that in the actual project very few mistakes, so I also here based on the command line to teach you to learn understanding.

  4. Git Command List

How can you tell if your git has been installed successfully? Please enter git in the command line and if the following prompts prove you have been installed successfully.

All of Git's operations commands start with git, listing some of the most common git commands, followed by an English explanation of the meaning of the command, which is not a difficult word, but try to look at it, but you still can't understand it without actually doing it. Let's take a practical action to describe the meaning of some of the common commands below.

  5. Git specific command

First, we'll create a new folder, create a new file in the folder (I'm new with Linux commands, Windows users can create them manually)

mkdir Test (Create folder test)

CD test (switch to test directory)

Touch a.md (new a.md file)

Here's a reminder: before you do any git, switch to the GIT warehouse directory first, which means you have to switch to the folder directory of your project first.

This time we first casually operate a command, such as Git status, you can see the following prompts (do not tangle color, configuration and theme is not the same):

This means that the current directory is not a Git repository.

 Git init

This time, the first command was used to initialize the Git warehouse, and after you enter Git init, you will be prompted:

You can see the initialization, so the test directory is already a git repository.

  git status

Then we enter the git status command with the following hints:

The default is directly in the master branch, the concept of branching will be mentioned, then the most important is the hint A.md file untracked files, that is a.md this file has not been tracked, has not been submitted in the Git repository, but also prompts you to use git add to operate your The file you want to submit.

Git status This command, as its name implies, is the most frequently used command, and it is recommended that you enter this command to view some of the status of your current Git repository.

  git add

It is suggested that the a.md file has not been submitted to the Git repository, at which point we can edit the a.md file casually and enter git add a.md and then enter git status:

This prompts the following file Changes to be committed, which means that a.md file waiting is submitted, and of course you can use git rm–cached this command to remove the cache.

 Git commit

And then we type git commit-m ' a ', what does this command mean? Commit is the meaning of the submission, the-M representative is to submit the information, executed the above order on behalf of us has been formally submitted for the first time.

When you enter Git status at this time, you will be prompted for nothing to commit.

  git log

This time we enter the git log command and we see the following:

The git log command can view all the resulting commit records, so you can see that a commit record has been generated, and the accompanying information at the time of submission is ' first commit '.

  git Add & git commit

See here a lot of people will have questions, I would like to submit a commit directly to the line, why do you want to add again first? First git add is the change to a "registers", you can understand that as a cache area, temporarily save your changes, and git commit Is the last real submission. The advantage of this is to prevent false submissions, of course, there are ways to combine the two steps into one step, but later on, suggest that the novice first step in step.

  Git branch

Branch is the meaning of the branch, the concept of the branch is very important, especially when team collaboration, assuming that two people are doing the same project, this time the branch is to ensure that the two people can work together the biggest weapon. For example, a, B two people are doing the same project, but different modules, at this time a new branch called A, b a new branch called B, so that a, b do all the code changes in their respective branches, not affect each other, wait until both of their own modules have done, and finally unified the branch merged.

After you execute git init initialize the git repository, it defaults to generating a master branch master, which is also your default branch, and basically a branch of the actual development formal environment where the master Branch will not easily operate directly, you can enter Git branch To view the current branch situation:

If we want to create a new branch on this basis, it's easy to execute Git branch A with a new branch called A, where branch A is exactly the same as Branch master, and we enter the current branch of Git branch view:

But you can see a * number before the master branch, that is, although a new branch of a is created, but the current branch is on master, and if we want to develop on branch A, we first have to switch to branch A, so the next step is to switch the branch

 Git checkout A

Execute this command and then enter GIT branch to view the next branch:

You can see now that we are in the branch is already a, this time a classmate can heartily in his new a branch to make code changes.

That someone said, I want to start a new switch, it is a bit of trouble, there is no one-step, smart:

 Git checkout-b A

The meaning of this command is to create a new branch of a and automatically switch to branch a.

git merge

A classmate in a branch code to write the joy, and finally his function completed, and the test is OK, ready to go online, this time it is necessary to merge his code into the main branch master, and then released. Git merge is a merge branch of the command used, in this case, you need to do two steps, the first step is to switch to the Master branch, if you are already in the need to switch, the second step of the implementation of git merge a, meaning to merge a branch of the code, no accident, This time the code for branch A is merged into the master branch. Why is there no accident? Because this time may have the conflict and merge failure, leave a baggage, this go to the back step of time to speak again.

  Git branch-d

There is a new branch, there must be delete branch, if this branch of the new wrong, or a branch of the code has been successfully merged into the master branch, then a branch is useless, need to delete, this time the implementation of Git branch-d A can be removed a branch.

Git branch-d

Sometimes you may delete a failure, for example, if a branch of code has not been merged into Master, you will not be able to perform git branch-d a can not delete, it would be smart to prompt you a branch and the unincorporated code, but if you want to delete, then execute git branch-d a You can force a branch to be deleted.

  git tag

We often have a version of the concept of client development, such as v1.0, V1.1 and the like, different versions must correspond to different code, so I generally want to add tags to our code, so that the v1.1 version out of a new bug, but do not know v1.0 is not a bug, with the label can be successfully switched to the v1.0 code, a pack test again.

So if you want to create a new label it's simple, like git tag v1.0 on behalf of me to create a new v1.0 tag in the current code state, enter git tag to view the history tag record.

You can see I've created two new tags v1.0, v1.1.

What to do if you want to switch to a tag? Also very simple, the implementation of Git checkout v1.0, so smooth switch to the v1.0 tag code state.

OK, all of the above are some of the most basic git operations, and are all in the local environment to operate, completely does not involve the remote warehouse, the next chapter will be remote GitHub warehouse as an example, explain how local with remote warehouse synchronization collaboration, and today is all about the most basic and simple git operation, step by step, Follow up on Git's high order and some of git's cool operations.

Welcome to the author's micro-signal:

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.