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
git
The installation is very simple, just go to git
the 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 64
for example :
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 git
options 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.
Step 2: Select the installation directory
The default installation path is c
disk, 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.
Step 3: Option installation component configuration
In this step, you can choose according to your own needs, all of which are some git
option configurations, and then click Next.
Step 4: Select the installation directory name
This step is git
the installation directory name, generally do not need to modify, just click Next.
Step 5: Choose your default editor
This step is git
the default editor of , and there are built-in 10
editors 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 Vim
editor, and then click Next.
Step 6: Choose the name of the trunk after creating the warehouse
After git
creating the name of the branch, the first is to git
choose, 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.
Step 7: Modify environment variables
The first one can only be used git
after 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 bash
git
git
git bash
cmd
windows
git
git
unix
windows
Step 8: Select
ssh
Execution File
This step can be done by default, and then click Next.
Note: This step is only available in newer versions.
Step 9: Select
https
Transfer
Open https
the connection to ensure the security of data transmission data, just follow the default selection, and then click Next.
Step 10: Configure the newline character at the end of the line
The first is checkout windows
style, commit unix
style line endings; the second is checkout as is, commit unix
style line endings; the third is checkout as is, commit as is.
Step Eleven: Configure the terminal to
git bash
work with
Select git
Terminal Type, select the default git bash
Terminal, and continue by clicking Next.
Step 12: Choose the default
git pull
behavior
Select git pull
Merge Mode, select Default, and click Next.
Step Thirteen: Choose a Credential Helper
Select git
Credential Manager for , choose the default cross-platform credential manager, and click Next.
Step Fourteen: Additional Options
For other configurations, select the default settings, and then click Next.
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 install
installation git
.
Step 16: Installing...
Step 17: The installation is complete
After the installation is complete, click fInish
End git
Installation.
Open the control panel (
win+R
entercmd
) and checkgit
the version information.
After the installation is complete, right-click any blank space and a new option will appear as shown below:
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 git
the built-in graphical tools are not perfect, TortoiseGit
the graphical operation tools provided by third parties are particularly important.
Download address: official website address
Domestic mirror: domestic mirror address
Installation process:
TortoiseGit
There is nothing to say about the installation process, just "next step" all the way.
I won’t go into details about TortoiseGit
how 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 init
command is used to create a new repository in a directory git
.
git clone
git clone
Copy a git
warehouse locally so that you can view the project or modify it.
Submit and modify
git add
git add
command 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 status
The command is used to check whether the file has been modified again since your last submission.
git diff
git diff
The 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 commit
The command adds the contents of the staging area to the local warehouse.
git reset
git reset
The 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 rm
command 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 mv
Command 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.
remote operation
git remote
git remote
Commands 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 -v
for example :
git fetch
git fetch
command 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 fetch
immediately after git merge
the remote branch to any branch you are in.
git pull
git pull
command is used to fetch code from remote and merge local version.
git pull <远程主机名> <远程分支名>:<本地分支名>
git push
git push
The 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 master
branch to the origin
master master
branch.
git push origin master