Git usage tutorial: basic operations of local warehouse


What is Git?

A distributed version control system, similar to SVN, but far more powerful than SVN ①Git can easily perform local version management, just like you have a local version management server We can choose to push the local version to the unified version management server at the right time ②Git will extract the complete image of the entire code warehouse every time, which is equivalent to a backup of the entire code warehouse. In this way, except for problems with the timing version server, we can directly use the local warehouse to restore it! Combined with the local version management function, if there is a problem with the remote version management server, we can still continue to write our own code, and when it recovers, we will submit our local version! Git was initially developed to better manage the Linux kernel, but now it has been widely used in various projects!


Install Git

If your system is Linux, directly open the shell and enter:

sudo apt-get install git

Of course, most systems are estimated to be Windows, which requires us to download a Git For Window online. You can download it from the following website: https://git-for-windows .github.io/Click Download to jump to Github and download the corresponding installation package!

Click to enter the page and download the following file

Or directly download the latest version 2.7.0: v2.7.0.windows.1Just follow the fool-like next step~ Next, you can find Git Gui and start playing with Git. However, if you switch to other platforms in the future, you will be unable to move forward without a graphical interface! So, if you are interested, let’s play with the command line, so that you can play Git normally even if you change the system in the future!


Playing with the Git command line

Of course, Git is definitely fun to play with GitHub, but let’s learn some local commands first! After you install Git, we can right-click anywhere and click Git bash to open our Git command line! You can click Git Init Here to create a code repository directly in the current directory, or click Git Gui to open the graphical operation page of Gui!


1. Create a code repository

Step 1: Configure our identity first, so that Git can know when submitting the code Who submitted it? The command is as follows:

git config --global user.name "coder-pig"
git congif --global user.email "779878443@qq.com"

After the configuration is completed, we can enter it again, excluding the name, and we can see that we have configured it successfully

Step 2: Find a place to create our code warehouse , then I created a new project: TestForGit, went to the project directory, right-clicked, opened our Git Bash, and typed the following instructions to complete the creation of the code warehouse! In addition, this code warehouse is actually used to save some information required for version management. The code we submit locally will be submitted to the code warehouse, so we can choose to restore to a certain version. Of course, if necessary, we can also Push the code saved in the code repository to the remote repository! For example, GitHub!

git init

With a simple code, the code warehouse is created! Continue to enter: ls - al. You can see that there is a .git folder in the lower directory, which is him!

You can also open the project directory, and you can also see the .git folder; if we want to delete the code repository, just delete this folder!


2. Submit local code

After creating the code warehouse, let’s talk about how to submit the code. We first use the add command to All the submitted content is added, and then commit actually performs the commit operation! The command example is as follows. You can slowly add it again and again. Of course, you can also submit them all. Just git add. and you're done! Let’s try creating a readme.txt file in the project directory. Write something casually, and then enter the following commands in sequence:

git add readme.txt
git commit -m "Wrote a readme file"

Try entering the command:

Of course, if you can add multiple files and then commit them all at once, but if we change a lot of files, we can git add .Add them all at once, but some are unchanged for hundreds of years or are automatically generated, such as lib, gen, bin folders, etc. We can create a file named .gitignore in the root directory of the code repository. file, then edit the contents and ignore the files that do not need to be submitted!

Then enter the content of the file to be ignored when submitting!

Then when we git add ., the files here will not be added. In addition, you may find it troublesome to write -m "xxx" after commit and want to be lazy. , but still write it down! What I entered are some statements submitted this time, such as what I have modified! It's like when you're writing code and you're lazy and don't write comments, after a few days you won't even know what you wrote...


3. Check the modifications

Okay Well, earlier we used git add to submit the entire project to the local warehouse. Next we change something and then use git status to view it. For the modified part, for example, we deleted the menu code and redundant menu-related packages in MainActivity.java!

He will prompt us which files have been changed but have not been submitted yet. If we want to see what has been changed specifically, we can use the git diff command. In addition, , press Q to return to the command line input!


4. Check the submission record

Of course, as our project progresses, the number of Commits will increase. Maybe you have already I forget what I modified every time I submitted it. It’s okay, Git will remember it for you. Use git log to view historical submission information! Type

git log

and press Enter:

Let’s take a small part of it to analyze:

commit defd8af52be5183dfceb3e5cf23f78ea47d013b0
Author : coder-pig <779878443@qq.com>
Date: Fri Jun 19 17:00:36 2015 +0800
MainActivity Delete Menu

The order is:

  • The version number corresponding to this submission

  • Submitter: Name Email

  • Submission time

  • The content of the submitted version modification: it is the xxx

## in our commit -m "xxx"
#5. Undo uncommitted changes

For example, we just submitted a version, and then wrote a bunch of things in a mess, and suddenly found that we accidentally deleted some things, and then ctrl + s to save Are you ready to cry at this time? But with Git, you only need a checkout command to undo the changes. Of course, this is the case if you have not added yet. For example, we can add a statement in MainActivity and then ctrl + s to save the code. !

Then type on the command line: git diff:

Well, here you can see what we changed, we You can go back and delete this line of code, but how do you change it if there are thousands of lines? So at this time we can use

git checkout src/com/jay/example/testforgit/MainActivity.java
and then we will magically discover that we newly wrote The code is gone! It will disappear immediately. If you don’t believe it, you can try it yourself

Of course, if we have already added it, then checkout will have no effect. We need to cancel the addition first. You can withdraw the commit by using the following command:

git reset HEAD src/com/jay/example/testforgit/MainActivity.java
git checkout src/com/jay/example/testforgit/MainActivity .java

6. Version rollback

The fifth point is that we taught you how to undo uncommitted changes, but after adding them, we What should I do if I want to go back to a previous version? In the fourth point, we can view our submission records through git log. We need to get a version number from here. Generally, we only need the first seven characters; in addition, in Git,

HEAD represents the current version, and the previous version is HEAD^. The next version is HEAD^^ and so on! Let’s take a look at the version history using Git Log first!

Let’s go back to the previous submitted version and type the following commands in sequence:

git reset --hard HEAD
git reset --hard HEAD^
git log

Look at our console at this time:

You can see that we have rolled back to the previous version. Of course, you can write directly like this:

git reset --hard ad2080c

It’s that simple! After rolling back, you suddenly regret it and want to roll back to the new version. But unfortunately, when you type git log, you find that the latest version number is no longer available. What should you do... It's okay, Git provides you with this "regret medicine". Git records every command you enter! Type:

git reflog

You will find that the version number is here:

Then type:

git reset --hard ad2080c

You can see that we are back to the latest version, just like that!

7. This section

This section introduces you to the project management tool Git to manage our local warehouse, and learns some basics Command line operation, I believe it will bring convenience to your project development. Of course, local is not enough. In the next section, we will learn how to host our project on GitHub! Stay tuned~