This article has participated in the third “topic writing” track at the Diggings Creators Camp. For more details, check out diggings program | Creators Camp

The installation

Take macOS as an example. Git officially offers a variety of friendly installation options, so you just need to choose one of them.

use

The basic use

Confirm correct installation

After installing Git, open the terminal tool and enter the following command to check the version of Git you have installed

git --version
Copy the code

The command is normal if information similar to the following is displayed

The git version 2.18.0Copy the code

Configuring User Information

After confirming that Git is installed properly, the first step is to configure git user information.

Because Git is also a common software, but it is a version control software, generally for software developers. Developers use Git for version control, which can be code, documentation, or anything else. Every time something changes, Git needs to know the user information of the changer, because it is part of the change information.

For example, Wilson added a readme.md file at 2021-08-16 23:12:48, which is a reasonable git history.

Not only does Git support local version management, it also supports networking, which means you can post your files online and their history can be shared with other Git users.

If I maintain the readme.md file with another person who knows my Git name, Wilson, then it’s easy to find me somehow. However, if I have never met someone online, it is necessary to add email information to git user information.

Therefore, before using Git, user name and mailbox are two basic configuration items that must be configured. We can enter the following two lines of instructions in the terminal to set our Git user information.

git config --global user.name=Wilson
git config --global [email protected]
Copy the code

Initialize the Git repository

After configuring user information, we can start to use Git to version our files. Then how to define the management scope of Git? This involves the concept of repository.

As we all know, every operating system has its own file system, macOS/Linux/Windows has its own file system, where directories (or folders) can generally be used as repositories to be managed by Git. When you use the following command in a directory, git creates a.git directory in the current directory to store all git control information about that directory.

git init .
Copy the code

Git will create a.git directory in the specified directory.

In a common file system. You can run ls -a to view all files (including hidden files) in the git directory (or select view hidden files in Windows Settings).

Check the git repository status

When a directory becomes a Git repository, you can start using a series of git commands. One of the most common is to check the status of the repository

git status
Copy the code

For example, the following is displayed in an empty directory

On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)
Copy the code

These three lines of output mean “currently in the master branch,” “no commit record yet,” and “Nothing to commit (create or copy files and use Git add to track them).”

These three lines of information contain the core common git features, so let’s move on to the common Git features section

Commonly used functions

branch

Branching is an interesting concept, kind of like a timeline. On the surface, the different Git branches look like copies, but the implementation is actually more sophisticated, which we’ll explore in an advanced chapter.

You can think of branches as copies. However, this copy is not only a copy of the file, but also a copy of all the history of the file.

For example, we are now in the default master (or possibly main) branch. We can create a new branch using the following command. The content of the new branch is a copy of the current branch at this moment.

git checkout -b dev 
Copy the code

After executing the above command, the console outputs

Switched to a new branch 'dev'
Copy the code

This means we created a new branch called dev and switched to the dev branch. At this point the dev branch is identical to the original master branch except for the name, which means they have the same files and the same history.

So you might think, well, what do we need branches for? Branches have many functions. For example, when different users modify files in the same directory, they can make changes and record them in their own branches, so that they can not interfere with each other. The use of branches will be discussed later.

Submit the record

So far, we haven’t made any changes to the file, so naturally there is no record of changes to the file. Let’s do something random, like create a readme.md file in the directory, and the contents can be empty.

Git has control over everything in its jurisdiction, so the readme.md file we just created has already been detected by Git. How do you know what Git is aware of the current file change

git status
Copy the code

The output of the above command would be

The status of output
On branch dev

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	README.md

nothing added to commit but untracked files present (use "git add" to track)
Copy the code

This command is used to check git’s perception of the current state of all files in the repository.

Let’s parse the above output line by line:

Readme.md: No content included in readme.md can be used to commit, only untracked files are shown above (using git add to track files).Copy the code

Git add is another important command that is repeatedly mentioned here, so let’s try it out.

tracking

The git add command can add files or directories specified in the command to the git track scope. So what does stalking mean? The files or directories in the repository are all under Git’s jurisdiction, but only the files that are included in the trace will have a history. Other files that are not included in the trace will “float” in the repository, and that’s it.

There is only one readme. md file in our repository, so let’s use Git Add to include it in git’s tracking scope, as follows

git add README.md
Copy the code

Git status = git status = git status = Git status = Git status

git status
Copy the code

The following information is displayed:

On branch dev

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

	new file:   README.md
Copy the code

The first two lines of information are the same as the previous status output, but this time the core content in the middle has changed, as explained below

Make the following changes to wait for commit: (use "git rm --cache name" to untrace) create a new file: readme.mdCopy the code

See, git sees everything we do in the file system, including creating a new file. Git has its own definitions for such operations, such as new file for new files.

When you use the git add file name to tell git to track a file, git will track every move of the file, including adding/deleting/modifying the file. Each of these actions is a record for Git.

How to explicitly tell Git to record a sequence of actions on a file that we want to record is to use another command, git commit.

Submit the record

When you use git commit, git records all changes to the files tracked (git only keeps a valid record of the files tracked) from the last Git commit to the current range. The content of the record is all changes to the files, including new additions, deletes, and modifications. One of the most common is the modification of the file, may be the name of the rename, or the contents of the file modification.

We try to make a commit by typing git commit on the command line. The first step is to use the vi editor like this

  1
  2 # Please enter the commit message for your changes. Lines starting
  3 # with '#' will be ignored, and an empty message aborts the commit.
  4 #
  5 # On branch dev
  6 #
  7 # Initial commit
  8 #
  9 # Changes to be committed:
 10 #   new file:   README.md
 11 #
Copy the code

You can press the I key to enter the input mode and enter the description of this submission. For example, we just created a new file called readme.md.

Press Esc to exit the input mode, then press: (note that the English input method is used), bring up the command input board, and enter wq to save and exit.

Immediately, the console displays the following

[dev (root-commit) ff44b28] add readme
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md
Copy the code

This indicates that we have successfully committed a Git record. The record ID is prefixed with FF44B28, which is different for each commit and is used to uniquely identify a commit.

You can use Git status to check the status of the repository

On branch dev
nothing to commit, working tree clean
Copy the code
There is nothing to commit on dev branch, the working tree is cleanCopy the code

This shows that in our current warehouse, file changes have been recorded by Git, and up to now, no new file changes have appeared, and the whole warehouse is clean and tidy.

conclusion

Now that we’re done with Git in a basic lifecycle, let’s take a look at the commands we’ve used:

git --version

git config

git init

git checkout
git status
git add 
git commit
Copy the code

The first of these is used to confirm the version information of Git, which is also a way to check that Git is installed properly.

The second is for configuring user information when you first use Git.

The third is used to initialize a directory (folder) as a repository that Git can manage. Init = initial; the result is a.git directory containing all the information about the current Git repository (branch information, commit history, etc.).

The last four are a set of common commands for basic Git operations, such as switching branches, viewing repository status, putting files in Git tracking, and committing records.

You’ve gotten started with git, but you’ll need to learn more advanced git commands if you want to do more productive and useful things with Git. If you’re interested in using Git, stay with git tutorials and other Git learning materials available online.