Master Git to make your code version control more handy!

foreword

Git is a very powerful version control system, and it is also a skill that programmers must master. It can help us manage and collaborate on code development more efficiently. This article will introduce the basic usage and common operations of git commands to help you better master this skill .


1. Installation

gitThe installation is very simple, just go to gitthe official website to download the corresponding system software: official website address

Of course, there is a high probability that you will not be able to download it on the official website. At this time, you can also choose: Ali Mirror

Take Windows 64for example :

insert image description here

Installation process

If you find it troublesome, you can choose "Next" all the way until the installation is complete. Of course, you can also choose to configure gitoptions for each step, as follows:

Step 1: Licensing Statement Information

There is nothing to say in the first step. It is mainly about some agreement-related content. Just click Next.

insert image description here

Step 2: Select the installation directory

The default installation path is cdisk, you can click to browse…change the directory, or modify it directly in the box, choose where to install according to your own needs, and then click Next.

insert image description here

Step 3: Option installation component configuration

In this step, you can choose according to your own needs, all of which are some gitoption configurations, and then click Next.

insert image description here

Step 4: Select the installation directory name

This step is gitthe installation directory name, generally do not need to modify, just click Next.

insert image description here

Step 5: Choose your default editor

This step is gitthe default editor of , and there are built-in 10editors in the installer for you to choose. If you choose another editor, you need to go to its official website to install it before proceeding to the next step. It is recommended to use the default Vimeditor, and then click Next.

insert image description here

Step 6: Choose the name of the trunk after creating the warehouse

After gitcreating the name of the branch, the first is to gitchoose, the default is master, the second is to decide by yourself, the default is main, you can also change it to other names, generally use the first, and then click Next.

insert image description here

Step 7: Modify environment variables

The first one can only be used gitafter installation and is not recommended; the second one is used from the command line and third-party software . Can be used from , command prompt ( ), and system environment variables, etc. , and is recommended; the third is from the command prompt using and optional tools. Selecting this will overwrite the tool and is not recommended for beginners. After selecting, click Next.git bashgit
gitgit bashcmdwindowsgit
gitunixwindows

insert image description here

Step 8: Select sshExecution File

This step can be done by default, and then click Next.

Note: This step is only available in newer versions.

insert image description here

Step 9: Select httpsTransfer

Open httpsthe connection to ensure the security of data transmission data, just follow the default selection, and then click Next.

insert image description here

Step 10: Configure the newline character at the end of the line

The first is checkout windowsstyle, commit unixstyle line endings; the second is checkout as is, commit unixstyle line endings; the third is checkout as is, commit as is.

insert image description here

Step Eleven: Configure the terminal to git bashwork with

Select gitTerminal Type, select the default git bashTerminal, and continue by clicking Next.

insert image description here

Step 12: Choose the default git pullbehavior

Select git pullMerge Mode, select Default, and click Next.

insert image description here

Step Thirteen: Choose a Credential Helper

Select gitCredential Manager for , choose the default cross-platform credential manager, and click Next.

insert image description here

Step Fourteen: Additional Options

For other configurations, select the default settings, and then click Next.

insert image description here

Step 15: Configure Experimental Options

The additional configuration options are all experimental functions, the technology is not yet mature, there may be some errors and the like, it is not recommended to check, and finally click the button below to start the installinstallation git.

insert image description here

Step 16: Installing...

insert image description here

Step 17: The installation is complete

After the installation is complete, click fInishEnd gitInstallation.

insert image description here

Open the control panel ( win+Renter cmd) and check gitthe version information.

insert image description here

After the installation is complete, right-click any blank space and a new option will appear as shown below:

insert image description here


2. Configure user information

git config --global user.name "xxx(新用户名)"
git config --global user.email "[email protected](新邮箱)"
git config --global user.password "xxx(新密码)"

View global configuration items

git config --list --global

View the specified global configuration items

git config user.name
git config user.email
git config user.password

Three, TortoiseGit installation

Since gitthe built-in graphical tools are not perfect, TortoiseGitthe graphical operation tools provided by third parties are particularly important.

Download address: official website address

Domestic mirror: domestic mirror address

Installation process:

TortoiseGitThere is nothing to say about the installation process, just "next step" all the way.

insert image description here

I won’t go into details about TortoiseGithow to use it in detail. If you are interested, you can go and find out.


Fourth, git basic operations (commands)

Create warehouse command

git init

git initcommand is used to create a new repository in a directory git.

insert image description here

git clone

git cloneCopy a gitwarehouse locally so that you can view the project or modify it.

insert image description here


Submit and modify

git add

git addcommand to add the file to the staging area.

Order describe
git add [file1] [file2] … Add one or more files to the staging area
git add [dir] Add the specified directory to the temporary storage area, including subdirectories
git add . Add all files in the current directory to the temporary storage area

git status

git statusThe command is used to check whether the file has been modified again since your last submission.

insert image description here

git diff

git diffThe command compares the differences between files, that is, compares the differences between the files in the temporary storage area and the work area.

Order describe
git diff [file] Show the difference between the staging area and the working area
git diff --cached [file] 或者 git diff --staged [file] Show the difference between the staging area and the last commit (commit)
git diff [first-branch]…[second-branch] show the differences between two commits

git commit

git commitThe command adds the contents of the staging area to the local warehouse.

insert image description here

git reset

git resetThe command is used to roll back the version, and you can specify to roll back a submitted version.

Order describe
git reset HEAD^ Roll back everything to the previous version
git reset HEAD^ 1.html Roll back the version of the 1.html file to the previous version
git reset 052e Fall back to the specified version
git reset --soft HEAD~3 Go back to the previous version
git reset --hard origin/master Roll back the local state to be the same as the remote state

git rm

git rmcommand is used to delete files.

Order describe
git rm 1.html Delete files from staging and workspace
git rm -f 1.html Forcibly delete the modified 1.html from the staging area and work area

git mv

git mvCommand is used to move or rename a file, directory or soft link.

Order describe
git mv [file] [newfile] file is the original file name, newfile is the renamed name
git mv -f [file] [newfile] If the new file name already exists, but you still want to rename it, you can use the -f parameter

log

git log

View commit history.

insert image description here


remote operation

git remote

git remoteCommands are used for operations on remote repositories.

Order describe
git remote -v Show all remote repositories
git remote show [remote] Display information about a remote warehouse
git remote add [shortname] [url] Add remote repository

Take git remote -vfor example :

insert image description here

git fetch

git fetchcommand is used to fetch a code repository from a remote.

Order describe
git merge Extract data from the remote repository and try to merge into the current branch

This command is executed git fetchimmediately after git mergethe remote branch to any branch you are in.

git pull

git pullcommand is used to fetch code from remote and merge local version.

git pull <远程主机名> <远程分支名>:<本地分支名>

git push

git pushThe command is used to upload and merge the local branch version to the remote.

git push <远程主机名> <本地分支名>:<远程分支名>

The colon can be omitted if the local branch name is the same as the remote branch name:

git push <远程主机名> <本地分支名>

The following command pushes the local masterbranch to the originmaster masterbranch.

git push origin master

Guess you like

Origin blog.csdn.net/Shids_/article/details/131221956