The basic configuration and use example of GIT under Linux system

Time:2021-6-1

Git config configuration file
1、 Git is already in your system. You will do something to customize your git environment. You only need to do these settings once; Even if you upgrade, they will be bound to your environment. You can also change these settings at any time by running the command.
      Git has a tool called git config, which allows you to get and set configuration variables; These variables control all aspects of GIT’s appearance and operation. These variables can be stored in three different locations:
1. / etc / gitconfig file: contains values applicable to all users and libraries of the system. If you pass the parameter option ‘– system’ to git config, it will explicitly read and write the file.
2. ~ /. Gitconfig file: specific to your users. You can make git read or write this particular file by passing the — global option.
3. Config file in Git directory (that is. Git / config): no matter what library you are currently using, it specifically points to the single library. Each level rewrites the value of the previous level. Therefore, the value in. Git / config overlaps the same value in / etc / gitconfig.
  In windows, GIT looks for the. Gitconfig file in the $home directory (for most people, under C: / / documents and settings \ $user). It also looks for / etc / gitconfig, although it is relative to the msys root. This could be anywhere you decide to install git when you run setup on windows.
 
2、 Your identity
2.1 When you install git, the first thing to do is to set your user name and e-mail address. This is very important because this information is used every git submission. It’s embedded in your submission forever:

Copy code

The code is as follows:

  $ git config –global user.name “John Doe”
  $ git config –global user.email [email protected]

  Again, you only need to make this setting once. If you pass the — global option, GIT will always use that information to handle everything you do in the system. If you want to use a different name or e-mail address in a specific project, you can run the command in that project instead of the — global option.
 
2.2 your editor
Now that your logo has been set, you can configure your default text editor, which git will use when you need to enter some messages. By default, GIT uses your system’s default editor, which is usually VI or vim. If you want to use a different text editor, such as Emacs, you can do the following:

Copy code

The code is as follows:

  $ git config –global core.editor emacs

 
2.3 your diff tool
Another useful option you may need to configure is the default comparison tool, which is used to resolve merge conflicts. For example, you want to use vimdiff:

Copy code

The code is as follows:

  $ git config –global merge.tool vimdiff

  Git can accept kdiff3, tkdiff, MELD, xxdiff, emergency, vimdif, gvimdif, ECMerge, and opendiff as effective merging tools. You can also set up a customized tool; See Chapter 7 for more information on this.
 
2.4 checking your settings
If you want to check your settings, you can use git config — list to list all the settings git can find there

Copy code

The code is as follows:

  $ git config –list
  user.name=Scott Chacon
  [email protected]
  color.status=auto
  color.branch=auto
  color.interactive=auto
  color.diff=auto
  …

 
You may see a keyword appear more than once because git reads the same keyword from different files (for example, / etc / gitconfig and ~ /. Gitconfig). In this case, GIT uses the last value for each unique keyword.
You can also check the current value of a specific keyword that git thinks. Use the following command git config {key}:

Copy code

The code is as follows:

  $ git config user.name
  Scott Chacon

 
2.5 getting help
If you need help when using git, there are three ways to get the man page help information of any git command:

Copy code

The code is as follows:

  $ git help <verb>
  $ git <verb> –help
  $ man git-<verb>

  For example, you can run the following command to get man page help for the config command:

Copy code

The code is as follows:

  $ git help config

  These commands are very friendly because you can access them anywhere, even offline. If the manual page and this book are still insufficient and you need personal help, you can try to use the # git or # GitHub channel (IRC. Freenode. Net) on freenode ircserver. These channels are regularly maintained by hundreds of professionals who are very familiar with GIT and will be happy to help you.
 
2.6 summary
You should have a basic understanding of what Git is and the differences between GIT and other CVCs you may use. You should also have a working git version with your personal identity in your system. It’s time to learn the basics of GIT.

Actual use record of GIT
1. Analysis of GIT submission code error

Copy code

The code is as follows:

$ git push origin master
Copy code

The code is as follows:

! [rejected] master -> master (non-fast-forward) 
error: failed to push some refs to ‘[email protected]:k6.git’ 
To prevent you from losing history, non-fast-forward updates were rejected 
Merge the remote changes before pushing again. See the ‘Note about 
fast-forwards’ section of ‘git push –help’ for details. 
[email protected]:~/work/k6_130708/k6$ git fecth 
git: ‘fecth’ is not a git command. See ‘git –help’. 
Git push error, because the latest code is not pulled first, the following operations are required:

Copy code

The code is as follows:

$ git fetch origin
$ git merge origin/master
$ git push origin master

If you don’t set your user name and e-mail address after installing git, you need to perform the following steps first:

Copy code

The code is as follows:

$ git config –global user.name “cbk”
$ git config –global user.email <a target=_blank target=”_blank” href=”mailto:[email protected]”>[email protected]</a>

This is very important because this information is used every git submission. It’s embedded in your submission forever:
Again, you only need to make this setting once. If you pass the — global option, GIT will always use that information to handle everything you do in the system. If you want to use a different name or e-mail address in a specific project, you can run the command in that project instead of the — global option.

Specifically, if you do not set your user name and e-mail address, you will not be able to push the code correctly.

2. Git tracks the submission history of a file
When tracking Android code, you sometimes find that a key file has been changed. You need to track and view all the commit records of the key file to understand the modification reason and process

Copy code

The code is as follows:

git log -p “file name”

Display the last commit of each line of this file to facilitate locating commit:

Copy code

The code is as follows:

git blame “file name”

Display log detailed modification record:
git show [log_ id_ Num], for example:

Copy code

The code is as follows:

git show 75704c8543b033619a80439ddb0fd69cc7cb172c

3. Git config for git initialization
1) The following command will modify the / home / [user name] /. Gitconfig file, that is to say, the following configuration is only visible to every SSH user, so everyone needs to do it.
The submitted code log will display the information of the submitted user

Copy code

The code is as follows:

git config –global user.name [username]
git config –global user.email [email]

Turn on color display in Git command

Copy code

The code is as follows:

git config –global color.ui true

2) . the following command will modify the / etc / gitconfig file, which is a global configuration, so admin can do it once.
  Configure some common git commands, alias

Copy code

The code is as follows:

sudo git config –system alias.st status #git st
sudo git config –system alias.ci commit #git commit
sudo git config –system alias.co checkout #git co
sudo git config –system alias.br branch #git branch

3) . you can also enter the work root directory and run git config – E. this will only modify the. Git / config file in the workspace, but it is not needed for the time being
The override order of GIT config file is 3) > 1) > 2)

4. Version fallback

Copy code

The code is as follows:

git reset –hard commit_id
Copy code

The code is as follows:

git checkout commit_id
Copy code

The code is as follows:

git clean –df commit_id