git easy tutorial

preface

Here is a tutorial for the little white people who don't know how to use git for the time being. Bloggers summarize while learning. Everyone makes progress together, and the big guys can ignore it~

1, What is git?

Git is currently the most popular version management system, and the most common application scenario is multi person collaborative development. Through git, you can view the whole submission process of the code, diff the differences between different commit s, and easily roll back to the historical version. The different states of GIT process and the switching between different states are shown in the figure above.

2, git Foundation

1. Create version Library

The version library is actually the corresponding Repository in the figure. The version control of the whole directory is realized through the version library. The steps to create a version library are as follows:
a. Create test directory;
b. git init initializes the version library, and you can see one more hidden file in the folder git.

2. git configuration user name, mailbox

git user name and email address are used to identify different users.
The – global parameter of git config command indicates that all Git warehouses on your machine will use this configuration. Of course, you can also specify different user names and Email addresses for a warehouse.

#Global configuration
git config --global user.name user name
git config --global user.email User mailbox

So how to set different user names and email s for different projects? Find the directory where the project is located git / folder, enter git / folder, and then execute the following instructions:

git config user.name user name
git config user.email User mailbox

3. git common operations

3.1 clone

#clone a version library from a remote host
git clone $url

3.2 add

git add . #Submit all workspace changes to index
git add <file> #Submit the file specified in workspace to index

3.3 commit

git commit -m "commit msg" #General usage after git add
git commit -a #If you modify the local file after the commit, you can directly add the modification to the staging area and generate a new commit
git commit --amend #Modify the last commit msg

3.4 reset

git reset --soft #Rollback staging area to a version Library
git reset --hard #Roll back the local workspace to a version

3.5 push

git push <Remote host name> <Local branch name>:<Remote branch name>

3.6 checkout

checkout can be used to copy files from the version library or staging area to the staging area, or branch related operations.

File operations related to checkout:

git checkout -- file #Overwrite local modifications with the status of the file in the staging area
git checkout HEAD~ test #Copy the test file of the previous version of the current version library to the temporary storage area and local

Branch operations related to checkout:

git checkout $branchname #Switch to a branch or tag, pay attention!!! The detached head may be generated, as shown in the following figure. At this time, an anonymous branch will appear when creating a commit. You can submit the modification through the newly created branch
git checkout -b $branchname  #Create a new branch

3.7 rebase

git commit --amend can only modify the latest bar. rebase can be used to modify historical commit and merge commit

git rebase -i master~3 #Enter editing status
git rebase --continue #Enter the next commit to be modified

3.8 pull

pull: retrieve the update of a branch of the remote host and merge it with the specified branch of the local host.

git pull <Remote host name> <Remote branch name>:<Local branch name>

3.9 diff

diff is mainly used to view the differences in different states. The detailed usage is as follows:

3.10 remote

git remote is used to manage remote host names.

git remote #View remote hostname
git remote -v #View remote host URL
git remote add <host name> <website> #Add host name
git remote rm host name #Delete host name
git remote rename <Original host name> <New host name> #Modify host name

reference

1,http://www.ruanyifeng.com/blog/2014/06/git_remote.html.
2,https://www.cnblogs.com/peng-lan/p/13038253.html.
3,https://blog.csdn.net/qq_2300688967/article/details/81094140.
4,https://www.liaoxuefeng.com/wiki/896043488029600/896827951938304.
5,https://mp.weixin.qq.com/s/6xhJ45Qb2aLO3wwSzIpAjg.

Added by areid on Mon, 07 Feb 2022 23:16:51 +0200