Recommended! Teach you how to use git)

Source: Internet
Author: User
Tags how to use git version control system

Source: Tu genhua's blog welcomes original articles to bole headlines

I. What is git?

Git is currently the most advanced distributed version control system in the world.

Ii. What is the main difference between SVN and git?

SVN is a centralized version control system, and the version library is concentrated on the central server. When working, it uses its own computers. Therefore, we must first obtain the latest version from the central server, after work, you need to push your work to the central server. A centralized version control system must be connected to the Internet to work. If the local area network is still available, the bandwidth is large enough and the speed is fast enough. If the network speed is slow on the internet, you will be confused.

Git is a distributed version control system, so it does not have a central server. Everyone's computer is a complete version library, so that you do not need to connect to the Internet during work, because the versions are all on your computer. Since each person's computer has a complete version library, how can many people collaborate? For example, if you change file a on your computer and another person changes file a on your computer, you only need to push the changes to the other party, then you can see the other party's changes.

Iii. How to install git on windows?

Msysgit is a Windows version of git, as follows:

Download one from the Internet and install it by default. After the installation is complete, find "Git-> git Bash" in the Start Menu, as shown below:

A similar command window will pop up, indicating that git is successfully installed. As follows:


After the installation is complete, you also need to set the last step. Enter the following in the command line:


Because git is a distributed version control system, you must enter the user name and email address as an identifier.

Note:The GIT config-global parameter indicates that all git Repositories on your machine use this configuration. Of course, you can specify different user names and mailboxes for a repository.

4. How do I perform this operation?

1. Create a version library.

What is a version library? A version library, also known as a repository, has the English name repository. You can simply understand a directory. All files in this directory can be managed by git, and each file can be modified, deleted, and tracked by git, this allows you to track history at any time, or "Restore" files at a certain time in the future ".

Therefore, creating a version library is also very simple, as shown in the following figure: I create a new testgit version library under the directory of drive D> www.

The PWD command is used to display the current directory.

1. Run git init to change the directory to a git-managed repository, as shown below:

At this time, you will have one more in the current testgit directory. git Directory, which is used by git to track and manage versions. do not modify the files in this directory manually. Otherwise, the GIT repository will be damaged. As follows:

2. Add the file to the version library.

First of all, make it clear that all version control systems can only track changes to text files, such as TXT files, web pages, and Code of all programs, and git is not listed, the version control system can tell you every change, but the binary files such as images and videos can also be managed by the version control system, but cannot track file changes, you can only concatenate the changes to the binary file, that is, you know that the image is changed from 1 kb to 2 kb.

Next let's take a lookDemoThe following is an example:

Create a new notepad file readme.txt in the testgit directory of the version library as follows: 11111111

Step 1: Run git add readme.txt to add it to the temporary storage area. As follows:

If there is no prompt like the above, it indicates that it has been added successfully.

Step 2: Run git commit to tell git to submit the file to the repository.

Now we have submitted a readme.txt file. We can run the git status Command to check whether there are any files not submitted, as shown below:

I have not submitted any files, but I will continue to change the readme.txt content. For example, I will add a 2222222222 line below and continue to use git status to view the results, as shown below:


The preceding Command tells us that the readme.txt file has been modified but not submitted.

Next, I want to see what the readme.txt file has changed. How can I check it? Run the following command:

Git diff readme.txt is as follows:


The contents of the readme.txt file are changed from 11111111 to 22222222.

After we know what changes have been made to the readme.txt file, we can submit it to the repository with confidence. Submitting the changes is the same as submitting the file. (the first step is GIT Add. The second step is: git commit ).

As follows:


Ii. Version rollback:

Sorry, we have already modified the file. Now I will continue to modify the readme.txt file and add another line.

The content is 33333333333333. Continue to execute the command as follows:

Now that I have modified the readme.txt file three times, how can I check the history? Now we can use the command git log to demonstrate the following:

The GIT log command displays the logs from the most recent to the farthest. We can see the last three submissions. The most recent one is that the increase is 333333. the last time is to add 222222 content, the first time is 111111 by default. if too much information is displayed above, we can use the GIT log-pretty = oneline command to demonstrate it as follows:

Now I want to use the version rollback operation. I want to roll back the current version to the previous version. What command should I use? You can use the following two commands. The first one is GIT reset-hard head ^. If you want to roll back to the previous version, you only need to change Head ^ to head ^, and so on. If you want to roll back to the first 100 versions, it is inconvenient to use the above method. We can use the following simple command: git reset-hard head ~ 100. The contents of readme.txt are as follows:

To roll back to the previous version, run the following command:

Check the content of readme.txt as follows: Run cat readme.txt to view

As you can see, the content has been rolled back to the previous version. We can continue to use git log to view the history information as follows:

We can see that we have not seen the increase of 333333 content, but now I want to roll back to the latest version, for example, how to restore the content of 333333? You can use the following command to roll back a version:

Git reset-hard version, but I don't know if I have switched off the command line or the version number of 333? How do I know the version number of the added 3333 content? You can obtain the version number by running the following command: git reflog:

We can see through the above display that the version number of the added content 3333 is 6fcfc89. Now we can run the command

Git reset-hard 6fcfc89 to restore. The demo is as follows:

We can see that the current version is the latest.

3. What is the difference between a workspace and a temporary storage zone?

Workspace:Is the directory you see on your computer, such as the files in testgit under the directory (except for the. Git hidden directory version library ). Or directory files that need to be created later will all belong to the workspace category.

Version Library(Repository):The workspace has a hidden directory. Git, which does not belong to the workspace and is a version library. The version library contains many things, the most important of which is stage (temporary storage zone), git automatically creates the first branch master for us, and a pointer head pointing to the master.

We have mentioned that using git to submit files to the version library involves two steps:

Step 1: Use git add to add files to the temporary storage zone.

Step 2: Use git commit to submit changes. In fact, all content in the temporary storage area is submitted to the current branch.

We will continue to use the demo for Demonstration:

In readme.txt, we can create a new file named test.txt with the content of test in the directory. We first use the command git status to check the status, as shown below:

Now we first use the GIT add command to add both files to the temporary storage area, and then use git status to view the status, as shown below:

Next we can use git commit to submit the job to the branch at a time, as shown below:

Iv. Git undo File Modification and deletion.

1. Undo modification:

For example, if I add a 555555555555 line in the readme.txt file, run the following command to view it:

Before submission, I found that the 5555555555555 content was incorrect, so I had to restore the previous version immediately. Now I can modify the following methods:

First, if I know what I want to delete, I will manually delete those files, add them to the temporary storage area, and then commit them.

Second, I can directly restore to the previous version using the previous method. Use git reset-hard head ^

But now I don't want to use the above two methods. What should I do if I want to use the undo command directly? First, you can check the current status with GIT status before undoing it. As follows:

Git tells you that git checkout-file can discard the workspace modification by running the following command:

Git checkout-readme.txt, as shown below:

Run git checkout-readme.txt to cancel all the modifications made to the readme.txt file in the workspace. There are two situations:

  1. After readme.txt is automatically modified, it has not been placed in the temporary storage area. You can use the Undo modification function to return to the same status as the version database.
  2. The other is that readme.txt has been placed in the temporary storage area, and then modified. Undo the modification and return to the status after the temporary storage area is added.

For the second kind of situation, I want to continue with the demo, for example, now I add a row of content to readme.txt as 6666666666666, I add it to the temporary storage area, and then add content 7777777. I want to use the undo command to bring it back to the status after the temporary storage area. As follows:

Note:The command git checkout-readme.txt-is very important. If there is no-, the command is changed to create a branch.

Ii. delete an object.

Assume that I want to upload a B .txt file to the testgitobject library and then submit the file. As follows:

As above: In general, you can delete the file directly in the file directory, or use the RM command: Rm B .txt above. If I want to completely delete the file from the version library, you can run the commit command to submit it again. The current directory is like this,


If I want to restore the file in the version library before the commit operation is completed, what should I do?

Run the following command git checkout-B .txt:

Let's take a look at our testgit directory and add three files. As follows:

5. Remote repository.

Before learning about it, register a GitHub account. Because the transmission between your local git repository and the GitHub repository is encrypted by Using SSH, you need to set one point:

Step 1: Create an SSH key. In the user's home directory, check whether there is. if there is an SSH directory, check whether there are id_rsa and files in this directory. If there are two files, skip the following command directly. If not, open the command line, run the following command:

Ssh-keygen-t rsa-c "[email protected]". Because I have run it once locally, it is available locally, as shown below:


Id_rsa is a private key and cannot be disclosed. is a public key. You can tell anyone with confidence.

Step 2: log on to GitHub, open the SSH keys page in "Settings", click "add SSH key", fill in any title, and paste the content of the file in the key text box.

Click Add key to view the added key.

  1. How do I add a remote database?

Now, we have created a local git repository and want to create a git repository on GitHub. We want to remotely synchronize these two repositories, in this way, the GitHub repository can be used as a backup, and other people can collaborate through the repository.

First, log on to GitHub and find "Create a new repo" in the upper right corner to create a new repository. As follows:

Enter repository nametestgitFor others, keep the default settings. Click "create repository" to create a new git Repository:

Currently, thistestgitThe repository is still empty. GitHub tells us that we can clone a new repository from this repository, or associate an existing local repository with it. Then, push the content of the local repository to the GitHub repository.

Now, according to the GitHub prompttestgitRun the following command in the repository:

Git remote add origin

All of them are as follows:

Push the content of the local database to a remote device. Using the GIT push command, the current branch master is actually pushed to the remote device.

Because the remote database is empty, when we push the master branch for the first time, the-u parameter is added, git not only pushes the content of the local master branch to the remote master branch, it also associates the local master branch with the remote master branch, and simplifies the command in the future push or pull. After the push is successful, you can immediately see on the GitHub page that the remote library content is already the same as the local one. The above username and password to enter GitHub are as follows:

From now on, as long as the local file is submitted, you can run the following command:

Git push origin master

Push the latest changes to the local master branch to GitHub, and now you have a real distributed version library.

2. How to clone from a remote database?

We learned how to associate a local database with a remote database first.

Now we want to know how to clone the remote database to a local database if there is new content?

First, log on to GitHub and create a new repository named testgit2. as follows:

As shown below, we can see:

Now, the remote database is ready. The next step is to clone a local database using git clone. As follows:

Then the testgit2 directory is generated under my local directory, as shown below:

6. Create and merge branches.

In version backfilling, you already know that each commit, git concatenates them into a timeline, and this timeline is a branch. Up to now, there is only one timeline. In git, this branch is called the master branch, that is, the master branch. In strict terms, the head does not point to the commit, but to the master. The master points to the commit. Therefore, the head points to the current branch.

First, create the dev branch and switch to the dev branch. Follow these steps:

Add the-B parameter to the GIT checkout command to create and switch, which is equivalent to the following two commands:

Git branch Dev

Git checkout Dev

Git branch: View branches. All branches are listed. An asterisk is added before the current branch. After all, we continue to demoss in the devsub-branch, and add another 7777777777777 line in readme.txt.

First, we will first download the readme.txt content, and then add the content 77777777, as shown below:

Now the dev work has been completed, and now we switch to master and slave nodes master, and the following content is continued:

Now we can merge the content on the dev branch to the master branch. On the master branch, run the following command git merge Dev:

The GIT mergecommand is used to merge the specified branch to the current branch, merge it, And then merge the readme.txt content. It can be seen that the latest commit of the dev branch is exactly the same.

Note thatFast-forwardGit tells us that this merge is a "fast forward mode", that is, direct the master to the current commit of Dev, so the merge speed is very fast.

After merging, we can delete the dev branch as follows:

The command for creating and merging branches is as follows:

View branch: git Branch

Create branch: git branch name

Switch branch: git checkout name

Create + switch branch: git checkout-B Name

Merge a branch to the current branch: git merge name

Delete branch: git branch-D name

  1. How to resolve conflicts?

Next, we will create a new branch, such as fenzhi1, add a row of content 8888888 in readme.txt, and then submit it, as shown below:

Similarly, we now switch to the master branch and add the content in the last row, with the content 99999999, as shown below:

Now we need to merge fenzhi1 on the master branch, as shown below:

Git uses <, =======, >>>>>>> to mark the content of different branches, here,

If you want to view the branch merge status, you need to use the GIT log. Command Line to demonstrate the following:

3. branch management policy.

Git usually uses the fast forward mode when merging branches. In this mode, after deleting a branch, the branch information is lost, now we can use the-no-FF parameter to disable the "Fast Forward" mode. First, let's demo:

  1. Create a Dev branch.
  2. Modify the readme.txt content.
  3. Add to the temporary storage area.
  4. Switch back to the master branch ).
  5. Merge Dev branches and run git merge-no-FF-M "comment" Dev
  6. View historical records

As follows:


Branch policy:First, the master branch should be very stable, that is, it is used to release the new version. Generally, it is not allowed to work on it. Generally, it works on the newly created dev branch, for example, to publish, or after the dev branch code is stable, it can be merged to the master branch.

7. Bug Branch:

During development, bugs are often encountered, so Bugs need to be fixed. In git, the branches are very powerful, and each bug can be fixed through a temporary branch, after the repair is completed, merge the branches and delete the temporary branches.

For example, when I receive a 404 bug during development, we can create a 404 branch to fix it. However, the work on the current Dev branch has not been submitted. For example:


It is not that I don't want to submit it, but that we cannot submit it when the work is half done. For example, it takes two days for me to submit this branch bug, however, it takes me five hours to complete the issue-404 bug. What should we do? Fortunately, GIT also provides a stash function to "hide" the current work site and resume work after it recovers. As follows:

So now I can fix the bug by creating the issue-404 branch.

First, we need to fix the bug on that branch. For example, I have fixed the bug on the master branch master. Now I want to create a temporary branch on the master branch. The demo is as follows:


After the repair is completed, switch to the master branch, complete the merge, and finally Delete the issue-404 branch. The demo is as follows:

Now, we are working on the dev branch.

The work zone is clean. Where can we go to the work site? You can run the GIT stash LIST command to view the details. As follows:

The work site is still there. Git has stored the stash content somewhere, but you can use the following two methods to restore it:

  1. Git stash apply is restored. After recovery, the stash content is not deleted. You need to use the GIT stash drop command to delete the content.
  2. Another method is to use git stash pop to restore and delete the stash content.

The demo is as follows:

8. multi-person collaboration.

When cloning from a remote database, git automatically maps the local master branch to the remote master branch, and the default name of the remote database is origin.

  1. To view the remote database information, use git remote
  2. Use git remote-V to View Details of the remote database.

The following is an example:


1. Push Branch:

The push branch is to submit all the local branches to the remote database. During the push, you must specify the local branch. In this way, git pushes the branch to the remote branch corresponding to the remote database:

Run git to push origin master

The code for readme.txt on githubis as follows:

The complete readme.txt code is as follows:


Now I want to push the latest readme.txt code from the repository to a remote database. The command is as follows:

We can see the success, and we can continue the readme.txt content on githubas below:

We can see that the push is successful. If we want to push it to another branch, such as the dev branch, we still use the GIT push origin Dev command.

In general, what branches should push?

  1. The master branch is the master branch, so it must be synchronized from time to time.
  2. Some bug fix branches do not need to be pushed to the remote. You can merge them to the master branch and then push the master branch to the remote branch.

2. Capture branches:

When multiple users collaborate, they will push their modifications to the master branch. Now we can simulate another colleague who can clone another directory on another computer (add the SSH key to GitHub) or on the same computer, create a directory named testgit2

But first, I need to push the dev branch to the remote, as shown below:

Go to the testgit2 directory and clone the remote database to the local directory, as shown below:


The generated directory is as follows:

To develop the dev branch, our partner must take the dev branch of the remote origin to the local machine. Therefore, you can use the command to create the local Dev Branch: git checkout-B Dev origin/dev

Now, you can develop on the dev branch. After the development is complete, push the dev branch to the remote database.

As follows:

My friends have pushed the submission to the origin/dev branch, and I have modified the same file in the same place under my directory file, when trying to push data to a remote database:

We can see from the above: Push failed, because the latest submitted by my partner conflicts with the push I tried, and the solution is very simple. We have already been prompted above, first use git pull to capture the latest commit from origin/dev, then merge it locally to resolve the conflict, and then push it.

Git pullThe connection between the local Dev Branch and the remote origin/dev branch is not specified. Set the link between Dev and origin/dev as prompted:

This timeGit pullThe merge operation is successful, but there are conflicts in the merge operation. The solution is the same as that in branch management. After solution, submit and then push:

Let's take a look at readme.txt.

Now that the manual solution has been completed, I need to submit it and then push it to the remote database. As follows:

Therefore, the mode of multi-person collaboration is generally as follows:

  1. First, you can try to use git to push origin branch-name to push your modifications.
  2. If the push fails, because the remote branch is earlier than your local update, you need to use git pull to try to merge.
  3. If a merge conflict exists, the conflict must be resolved and submitted locally. Use git to push origin branch-name.

GitCommon commands are as follows:

Mkdir: XX (create an empty directory XX indicates the directory name)

PWD: displays the path of the current directory.

Git init converts the current directory into a manageable git repository and generates a hidden. Git file.

Git add XX adds the XX file to the temporary storage area.

Git commit-m "XX" refers to comments behind the commit file-M.

Git Status view repository status

Git diff XX view XX File Modified those contents

Git log view history

Git reset-hard head ^ Or git reset-hard head ~ Roll back to the previous version

(If you want to roll back to version 100, use git reset-hard head ~ 100)

Cat xx

Git reflog view the version number ID of the History

Git checkout-xx removes all modifications to the XX file in the workspace.

Git rm xx Delete XX File

Git remote add origin associates with a remote Library

Git push-U (not required after-u is used for the first time) origin master pushes the current master branch to the remote database

Git clone clone from remote Library

Git checkout-B Dev: Create the dev branch and switch to the dev branch.

Git branch: view all current branches

Git checkout master switch back to master Branch

Git merge Dev combines Dev branches on the current Branch

Git branch-D Dev Delete Dev Branch

Git branch name: Create a branch

Git stash hides the current job and continues to work after it recovers from the site.

Git stash list view the list of all hidden files

Git stash apply restores hidden files, but the content is not deleted.

Git stash drop delete file

Git stash pop also deletes files while restoring files

Git remote view remote database information

Git remote-V: view detailed information about the remote database

Git push origin master git will push the master branch to the remote branch corresponding to the remote Library

Recommended! Teach you how to use git)

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: 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.