Git introduction

1.1. What is Git

Git is an open source distributed version control system developed in C language, which can handle version management of projects from very small to very large efficiently and quickly.

1.2. Centralized and distributed version control systems

Centralized version control system: there is a central server where all the files are stored. To modify the content, the files must be pulled from the central server and pushed to the server early after modification. If the central server if the file on the central server is missing all of them can’t read that file.

Distributed version control systems: Distributed version control systems have no “central server” at all. Everyone has a complete version library on their computer, so that when you work, you don’t need to be connected to the Internet because the version library is on your own computer. Since everyone has a complete version library on their computer, how can multiple people collaborate? For example, if you change file A on your own computer, and your colleague changes file A on his computer, both of you just push your changes to each other, and each of you can see the other’s changes.

1.3. The Git installed

Yum Installation

yum install -y gti
Copy the code

Uninstall the git

yum remove git
Copy the code

Source code compilation and installation

1. Download source code

Source address: mirrors.edge.kernel.org/pub/softwar…

Yum install -y wget wget -o/TMP /git-2.21.0.tar.gz https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.21.0.tar.gzCopy the code

2. Decompress and compile

#Install build dependency
yum install -y curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker

#Unpack theTar -zxf/TMP /git-2.21.0.tar.gz -c/TMP/CD/TMP /git-2.21.0
#Verify dependencies and set the installation path
./configure --prefix=/usr/local/git

#Compile the installation
make && make install
Copy the code

3. Configure global environment variables

#Delete an existing Git
yum remove git

#Configuring environment Variables
vim /etc/profile

# GIT_HOME
GIT_HOME=/usr/local/git
export PATH=$PATH:$GIT_HOME/bin

#The refresh
source /etc/profile
Copy the code

Local version library

2.1. Create a version library

Create account

Because Git is a distributed version control system, you must differentiate between different machines when working with it. This can be done by configuring the machine name and mailbox. Git will also be prompted for configuration upon initial use.

git config --global user.name "Your name"
git config --global user.email "[email protected]"
Copy the code

In actual use, you can replace Your Name and email@example with Your actual Name and email address.

Repository and warehouse, English name repository, is a folder, the folder in the file can be git up and management can track git, each file modification and deletion at any time can be restored.

Create a version library

mkdir /home/repository
cd /home/repositry
Copy the code

Initialize the warehouse

git init
Copy the code

After initialization, there will be a.git directory in the current directory. This directory is used by Git to track and manage the repository. Do not modify the files in the directory.

Add the file to the version library

Write a file called readme.txt

Git is a version control system.
Git is free software.
Copy the code

Be sure to place Hello.sh in the repository folder, which is git’s repository, and execute the following command

git add readme.txt
git commit -m "wrote a readme file"
Copy the code

Git commit: -m: git commit: -m: git commit: -m

Parse the output of the COMMIT

Git commit results in the following

[root@Centos repository]# git commit -m "wrote a readme"
[master (root-commit) ffbd581] test
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 123.sh
Copy the code

Test is an argument to -m

1 file changed, 0 insertions(+), 0 deletions(-)

The string ffbd581 is the commit Id. The COMMIT Id uniquely corresponds to a commit.

Question:

Fatal: Not a git repository (or any of the parent directories)

Git commands must be executed in the Git repository directory (except Git init), and it makes no sense to execute outside the repository.

2. Run git add readme. TXT. Fatal: pathSpec ‘readme. TXT ‘did not match any files.

When you add a file, the file must be in the directory of the staging area. You can run mv and copy commands to copy the file to the staging area

2.2. View warehouse status

Modify the readme. TXT file to the following content

Git is a distributed version control system.
Git is free software.
Copy the code

Git status You can view the status of the repository

Git diff file changes

[root@CentOS repository]# git diff readme.txt diff --git a/readme.txt b/readme.txt index 46d49bf.. 53d4271 100644 -- a/readme. TXT +++ b/readme. TXT @@-1,2 +1,2 @@ -Git is a version control system  version control system. Git is free software.Copy the code

As you can see from the command output above, we added a Distributed word in the first line.

2.3. View the commit log

When you need to go back to a certain version, you can use the git log command to view the file commit history to view the commit information

[root@CentOS repository]# git log -1
commit 82a3a95429d4979aee99691eb4fd880a35f59ef0
Author: root <[email protected]>
Date:   Mon Feb 15 17:37:51 2021 +0800

    insert distributed
Copy the code

Commit 82 a3a95429d4979aee99691eb4fd880a35f59ef0 behind a string of characters is full commit id;

“Author” is the Author of this commit, which is the user.name we configured in git config.

The final output of “Insert Distributed” is the information we added during the submission.

Git log -p displays the changes between commits. Git log -1 displays the latest update.

The COMMIT ID is a hexadecimal number calculated by SHA1 to prevent collisions caused by duplicate ids.

2.4. Roll back the version

First, Git must know what the current version is. In Git, the current version is denoted by HEAD, that is, the latest commit. The previous version was HEAD^, the previous version was HEAD^, and the previous version was HEAD^^

Go back to the original version without distributed words

git reset --hard HEAD^
Copy the code

Solution if you want to go back to the distributed version after rolling back to the version without the Distributed

If you haven’t turned off your computer, scroll forward to find the commit ID of the target version

git reset --hard  82a3a9542
Copy the code

What if I can’t find it

Run git reflog to record the command executed each time. Then you can check the commit ID of the required version and roll back

[root@CentOS repository]# git reflog
a51529a HEAD@{0}: reset: moving to HEAD^
069f6b5 HEAD@{1}: reset: moving to HEAD^
82a3a95 HEAD@{2}: commit: insert distributed
Copy the code

2.5. Undo and Delete

Undo file changes in the workspace

If a file in your workspace has been tampered with and hasn’t been committed yet, you can use the Git checkout command to retrieve the file from before the change.

git checkout -- [filename]
Copy the code

The principle is to first find the staging area, if the file has a staging version, then restore that version, otherwise restore the last committed version.

Note that once a workspace file change is revoked, it cannot be retrieved.

Undo files from the staging area

If you accidentally add a file to the staging area, you can undo it with the following command.

git rm --cached [filename]
Copy the code

The above command does not affect content that has already been submitted.

delete

The git rm command is used to delete a file. If a file has been committed to the repository, you never have to worry about deleting it by mistake, but you can only restore the file to the latest version, and you will lose the changes you made since the last commit.

Git rm nameCopy the code

Remote warehouse

3.1. Clone the remote version library

Git clone is the command used for cloning

Git Clone remote repository addressCopy the code

By doing this, the remote repository will be copied locally, and by default will be cloned to the sample folder corresponding to the sample specified in the remote repository address.

You can specify another directory as required

Git Clone Specifies the directory where the remote version library is locatedCopy the code

The instance

Clone GitHub busybox project to /root/busybox

git clone https://github.com/billie66/TLCL.git /root/busybox
Copy the code

3.2. Add a remote version library

Git remote add git remote add

Git remote add Specifies the name of the remote repositoryCopy the code

The instance

Add the remote repository and name it git

git remote add git /tmp/educoder.git
Copy the code

3.3. Push local content to remote repository

When local content is pushed, all content that is not pushed to the remote repository is referred to the remote repository. The command it uses is Git push

Git push [option] Remote repository name Local branch name Remote branch nameCopy the code

Options:

-u: establishes the mapping between the local master branch and the remote master branch. If the master branch is pushed next time, the name of the remote branch can be ignored.

#For the first time to push
git push -u origin master master

#Push again
git push origin master master
Copy the code

Iv. Branch management

4.1. Create and switch local branches

Creating a local branch

The command used to create a local branch is Git branch

Git branch creates a new branch:

git branch new_master
Copy the code

Switching local branches

Use the git checkout command to switch local branches

Git checkout branch name, switch to new_master

git checkout new_master
Copy the code

Create and switch to the new branch you created

Use Git checkout -b to create and switch to the new branch you created

Git checkout -b branch name

git checkout -b hello_master
Copy the code

4.2. Delete the local branch

To delete a local branch, run the git branch command and use the -d parameter

Git branch -d Specifies the name of the branch you want to delete

Delete the hello_master branch

git branch -D hello_master
Copy the code

4.3. Pull the remote branch to the local

To pull the contents of the remote repository to a local directory, run the git pull command

Git pull repository name Remote branch name local branch nameCopy the code

Options:

-f: When the remote branch conflicts with the local branch, you can resolve the conflict and forcibly pull the remote branch to overwrite the local branch. -f: Forcibly pull the remote branch to override the local branch

Pull content from the master branch of remote repository Origin to the local master branch

git pull origin master:master
Copy the code

4.4. Delete the remote branch

There are two ways to delete a remote branch:

1. Push an empty branch to the remote branch for deletion.

Git push remote host name: The local branch name is not specified before the colon, so it is an empty branch;

Delete the remote Develop branch where origin is the remote host name.

git push origin :develop
Copy the code

2. Use the delete parameter to delete the remote branch.

To delete a remote branch, use the delete parameter in the following format: git push remote host name –delete remote branch name;

Delete the remote Develop branch where origin is the remote host name.

git push origin --delete develop
Copy the code

4.5. Local branch merge

To merge branches, run the git merge command. The command is in the following format: git merge Branches to merge.

For example, to merge the Develop branch into the Master branch, run the following command

git merge develop
Copy the code

Branch merges can also be divided into normal merges and fast forward merges

Fast-forward merging

By default, Git performs a “fast-farward merge,” which points the merged branch directly to the branch that needs to be merged. The diagram below:

The normal merger

Fast-forward and merge have the advantage of simplifying the repository history and keeping it linear. The downside is that we can’t look at this evolution of the repository based on the history that has been merged. Git provides the — no-ff option to force a new commit.

git merge --no-ff develop
Copy the code

Five, label management

5.1. Creating labels

Switch to the branch and tag it with git tag.

Git branch new_master Git tag v1.0Copy the code

The default label is the latest commit. To label historical commits, you need a COMMIT ID.

1. View the COMMIT ID

[root@CentOS repository]# git log
commit 41f59c3782adec55666742c2714c1ab36bebd082
Author: root <[email protected]>
Date:   Thu Feb 18 15:30:34 2021 +0800

    HelloWorld

Copy the code

2. Play tag

41 f59c3 git tag v2.0Copy the code

The label is always tied to a COMMIT, and if the commit appears on both the Master and dev branches, the label will be visible on both branches.

Create labels with descriptive content

Git tag -a v2.0 -m"HelloWorld" 41f59c37Copy the code

5.2. View labels

Check the label

[root@CentOS repository]# git tag
v1.0
v2.0
Copy the code

View the description of the label

Git show v2.0Copy the code

5.3. Operate labels

Remove the label

Git tag -d Specifies the tag nameCopy the code

Push labels to remote repositories

Git push remote repository name tag nameCopy the code

Push all labels at once

Git push repository name --tagsCopy the code

Deleting a Remote Label

If a label has been pushed to a remote device, you need to delete the label from the local device first:

Git tag -d Specifies the tag nameCopy the code

Then remote delete, the principle is to push a blank label name overwrite the label to be deleted.

Git push origin: name of the remote tagCopy the code