Git is a great solution-2. Detailed explanation of Git local operation commands

Source: Internet
Author: User
Tags git commands

Git is a great solution-2. Detailed explanation of Git local operation commands
Git is a great solution-2. Detailed explanation of Git local operation commands

Tag: Git

Introduction

The previous section explains some concepts about Git, the introduction of Git, the four components of Git, the status of Git files, and
Git download and installation. The obvious difference between Git and SVN is that Git can be installed without the network.
Control, this is because every computer in Git has a local version library, and the remote repository only serves as an exchange for modification.
A tool! Even if we lose this tool, we can also work, but it is not convenient to switch and modify, for example, SVN, remote
The server is down... So most of the time we use Git, we are doing some local operations on Git.
Some Questions about remote server branch merge. In this section, we will first familiarize ourselves with some common commands for local operations! This section
The description is as follows:

1. Set your identity information ( Git config) 2. Get help ( Git help3. Create a local code repository ( Git init4. Put the file in the temporary storage area ( Git add) 5. Submit the content of the temporary storage area to the local warehouse ( Git commit6. view the status of the current workspace and temporary storage area ( Git status7. view the differences between the workspace and the temporary storage area ( Git diff) 8. View submitted change records ( Git log) 9. Restore the file -- not add ( Git checkout) 10. File recovery-added without commit ( Git reset) 11. File recovery (commit) -- version rollback ( Git reset HEAD ^) 12. view each input command ( Git reflog) 13. Git command alias ( Git config-xxx alias. xxx) 14. Git command auto-completion ( Press the Tab twice)

Okay. Start this section (install Git first !)~

1. Set your identity information

After installing Git, the first thing we need to do is to configure our identity information as part of our own team collaboration.
For example, if someone modifies a file, enter the following command:

git config --global user.name "coder-pig"git config --global user.email "[email protected]"

After the configuration is complete, remove the "" part of the information and enter the preceding command one more time to check whether the configuration is successful.

You can also enter the following command to view all Git settings:

git config --list

In addition, the configuration information set above is global. Generally, we work in the company, and most of them work in collaboration with the company.
Project, you can set global configuration here, useGlobal <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Release + 7Ev6OsxMfDtDxiciAvPg0KxOO/ydLUtaW2wLXEzqrV4rj2z + release/CyvbD/MHuo7o8L3A + DQo8cHJlIGNsYXNzPQ = "brush: java;"> git config --local user.name "coder-pig"git config --local user.email "[email protected]"

You only need to change globalLocalYou can ~ In this case, the git configuration of this repository will not be the same as the global configuration,
This can avoid unnecessary troubles!

2. Get help

Like other command lines, Git also has the help command. When we have never seen it, or forget to use it
Command, we can type:

git help init

Change init to the command you want to query! For exampleGit help add!

In Windows, a Git Manual (Manual) page will be opened. You can view the usage of commands like this.
In Ubuntu, the command line output is as follows:

Of course, you can also find the corresponding commands in the Git official manual!

3. Create a local code repository

You can directly type the following command to create a project with a Git Repository:

git init GitForTest

Change GitForTest to the project name you want to create! Next we can go to the newly created Project
Directory, You need to modify it to make the hidden file visible, you can see in the. git folder
Dongdong is the stuff of our git repository. Remember not to modify or delete it at will! (You also
You can enter:Ls-ahTo view hidden files)

If you already have a project and want to add a Git repository on the basis of the previous project

Go to the folder directory of the current project through the command line or git bash, and enter the following command:
Add a local Git repository for your project:

git init
4. Put files in the temporary storage area

As mentioned above, if the file is not added with version control, the file is inUntracked
(Not tracked) status, we can use the git add command to add the file to the temporary storage area, the file will change
ChengTracked(Tracked) status! We can use the following commands to add files to the temporary storage area one by one:

git add README.md

If you want to add a large number of files, it may be difficult to add one by one. You can add multiple files at a time:

1) Add the modified or deleted file information in all tracked files to the Git staging area, and the untracked files will not be processed!

git add -u

2) Add the modified or deleted file information in all tracked files to the Git repository, and untracked file information will be processed.
Also join the Git staging zone

git add -A

3) add all the files in the current workspace to the Git staging zone.

git add .

In addition to the above three methods, git also provides us with an interactive interface mode. You can type:

git add -i

The process is as follows::

1. I first created two files in the GitForTest folder.
2. Type git add-I, enter, type 4, select the file to add untracked
3. He listed untracked files for us, and then we added the files according to the serial number.
4. input? A prompt will pop up, and press enter to end the selection!
5. Enter git add-I again, and enter 4. The untacked file does not exist!

Of course, there are several other commands, limited by space. If you are interested, you can study them on your own!

5. Submit the content of the temporary storage area to the local warehouse.

You can use the git commit-m "xxx" command to submit the content of the temporary storage area to the repository.

Git commit-m "modified xxx"

-M is the description of this submission, and "xxx" is the description. You should not be lazy and save it if you do not
Input-m "xxx" will also let you enter Vi/Vim to compile the declaration information ~ So it is recommended that
To submit the content!

In addition, our project may have several hundred years of unchanged or automatically generated files, such as lib,
Gen, bin folder, etc. We don't need to commit these all every time. We can
Create a file named. gitignore in
Write, then the commit will automatically ignore these files ~ :

6. view the status of the current workspace and temporary storage area

We can use the git status Command to view the current situation of the workspace and the temporary storage area, for example, what is
The comparison between the file and the file in the temporary storage area has changed. Do you want to add it? For example, there is something in the temporary storage area to add
But you haven't submitted it yet. type the following command:

git status

For example, I modified the README. md file here, but I didn't add it yet:

After adding the file:

Submit the content of the temporary storage area

Well, it's very simple. In addition, you can use the following commands to make the results output in a short form ~

git status -s
7. view the differences between the workspace and the temporary storage area.

Above, we can use git status to get the status of the current workspace and cache zone, only the status,
If you want to view the changed content, enter the following command:

git diff

In this way, you can see the changes made in the current workspace and the temporary storage area!
PS: we added a statement in the README. md file, and then typedGit diff!

8. View submitted changes

Do you still remember the previous online shopping example? We can find our order records in my orders,
Similarly, in Git, we can also view all commit records! You can enter the following commands:

git log

Of course, you can also call the following command to get more streamlined results

git log --oneline

If the above does not meet your requirements, you can refer to: Viewing the Commit History
Customizes logs, such:

9. File recovery-not added

You can right-click to delete a file, or enter the command line and type rm xxx. xxx to delete the file,
However, only the files in the current workspace are deleted. This file still exists in the temporary storage area.
If you type git status, you will see the following results:

Git tells you that the file in the workspace has been deleted, and you have two options:

1) delete the files in the temporary storage area, and enter:

git rm "xxx.xxx"git commit -m "xxx"

2) If the file is deleted by mistake and the file in the temporary storage area is restored to the work area, you can enter:

git checkout -- xxx.xxx

Duang! The deleted file is back ~
Of course, the preceding checkout is not only applicable to accidental file deletion. When you change a file to a non-visual one,
You suddenly regret it, but you have saved the code many times by ctrl + s. You can use the above command to return
The initial appearance of this file! (The premise is that you haven't added it yet !)

10. File recovery-added and not commit

If you have added the file to the temporary storage zone using git, you can directly use the checkout File
What's the role! We need to use the git reset command to abolish this modification record (version rollback), so that the current file
Return to the status of the last submission! Type:

git reset HEAD xxx.xxx

Then call:

git checkout -- xxx.xxx

The file can be restored to its original state!

11. File recovery (commit)-version rollback

If our file has been modified, and you regret it for no reason, you want to restore it to the last time.
The file at the time of commit, or the previous one, you may have started at this time, but Git mentioned it for us.
For the time machine (version rollback), we can use the following command to roll back to the previous version:

git reset HEAD^

Well, after typing git log, we can see that the version has been rolled back to the previous version!
If it is the previous version, you only need to add more ^, and then the previous version continues to add ^, and so on!
Of course, in addition to the above form, you can also roll back based on the version number. For example, here I return to the first version.

git reset --hard 8c3f91f

Hey, no pressure, you suddenly regret it and want to return to the new version... Okay, it's also above.
Command: But you can change the version number to the latest version number of commit!

git reset --hard cf2d155
12. view each input command

You may say to me, "Well, I just shut down the command line, the latest version.
The git log cannot find the latest version number. Can't I return to the future?
"Fortunately, you only need to enter the following commands to record every command you input in Git:

git reflog

Get the version number and git reset it ~
Note that git reflog is not always saved,
Git regularly cleans up "unusable objects", so don't expect to find another submission several months ago!

13. Git command alias

If you want to be lazy and want to knock a few letters less, you can set an alias for the command, and then type the alias to call the corresponding command,
For example, set status to st:

14. Automatic completion of Git commands

Press the Tab twice when you enter the Git command!

Summary:

This section gives you a detailed explanation of a wave of local Git commands, which can meet your daily needs. To learn this section, we recommend that you
Build a warehouse by yourself and follow the instructions step by step. I believe you will benefit a lot and get familiar with it when you think about it! Next we will
Describes branch related concepts and instructions! Thank you ~

 

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.