This article brings you basic knowledge about the use of git, including basic operations of git, branch operations, change submission operations, etc. I hope it will be helpful to everyone.
#1. Set SSH Key
Set SSH Key so that the device can have permission to access the code warehouse in the account
$ ssh-keygen -t rsa -C "your_email@example.com"
Copy after login
-
"your_email@example.com"
Set it as the registered email address of your GitHub account
- The id_rsa file is the private key, and id_rsa.pub is the public key.
$ cat ~/.ssh/id_rsa.pub
Copy after login
ssh-rsa The content of the public key your_email@example.com
- Then copy the public key and add it to the account. Pay attention to the # in front. ##ssh-rsa
Also copy it
Avatar》Settings》SSH Key》new SSH Key
Next verify it, if the word "successfully" appears, it means success
$ ssh -T git@github.com
Enter passphrase for key '/c/Users/MYPC/.ssh/id_rsa':
Hi abc! You've successfully authenticated, but GitHub does not provide shell access.
Copy after login
2. Basic git operations
2.1 git clone existing warehouse
$ git clone git@github.com:hirocastest/Hello-World.git
Copy after login
You will be asked to enter the open key set on GitHub. Password, after successful authentication, the warehouse will be cloned into the current directory.
2.2 git add adds the file to the staging area
After the code is written, add the code to the system's staging area
$ git add 文件夹/文件
Copy after login
2.3 git commit Save the history of the warehouse
The git commit command can actually save the files in the current staging area to the history of the warehouse. Through these records, we can restore files in the working tree.
$ git commit -m "记录一行提交信息"
Copy after login
m indicates an overview of this submission. If you want to record detailed information, remove -m-
2.4 After git push
, just Execute the
push command, and the warehouse on GitHub will be updated
$ git push
Copy after login
2.5 git init initializes the warehouse
clone method to create a warehouse, no execution is required
init Operation. If you want to set a local file as a warehouse, you need to perform an init operation
$ mkdir git-tutorial
$ cd git-tutorial
$ git init
Copy after login
2.6 git status to view the warehouse status
$ git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
Copy after login
The result is that we are under the master branch and there is nothing to submit Content
2.7 git log View submission log
git log command can view the logs submitted in previous warehouses. Including who made the commit or merge at what time
$ git log
commit 5dbbff6e009abb8a6cc44187c93b694f94fbf82a (HEAD -> main, origin/main, origin/HEAD)
Author: ywm <ywm_up@qq.com>
Date: Sun Feb 28 17:17:00 2021 +0800
Copy after login
If you only want to display the first line of the commit information, you can add
-- pretty=short after the git log
command
$ git log --pretty=short
Copy after login
2.8 git diff Check the difference before and after the change
Execute
git diff Check the difference between the current working book and the staging area
$ git diff
Copy after login
3. Branch operation
You can create multiple branches and perform completely different jobs at the same time. Wait for the branch job to be completed before merging it with the master branch. Through the flexible use of branches, multiple people can perform concurrent development efficiently at the same time.
3.1 git branch displays the branch list
There is an “*” (asterisk) on the left side of the master branch, indicating that this is the branch we are currently on.
$ git branch
* master
Copy after login
3.2 git checkout creates and switches branches
Create and switch to branch feature-A
$ git checkout -b feature-A
Copy after login
In fact, the above command is equivalent to the following two commands
$ git branch feature-A
$ git checkout feature-A
Copy after login
Switch back to branch main
$ git checkout main
Copy after login
Switch back to the previous branch
$ git checkout -
Copy after login
Do this: create a new branch feature, and modify the README on the new branch. md, and add and commit-
It can be proved through actual operation that as long as multiple branches are created, multiple functions can be developed at the same time without affecting each other
3.3 git merge merge branches
Add the --no–ff parameter when merging to save the previous branch history
$ git merge --no-ff feature
Copy after login
The editor will then start to enter the merge submission information
3.4 git log --graph View branches as icons
$ git log --graph
Copy after login
4. Change commit operations
4.1 git reset Backtrack historical versions
$ git reset --hard 目标时间点的hash值
Copy after login
View the operation log of the current warehouse through
git reflog to find the hash value before the backtracking history. As long as git's GC (garbage collection) is not performed, the recent historical status can be retrieved at will through the log. Even if the developer performs a git operation by mistake, he can basically use the git reflog command to restore to the original state.
$ git reflog
Copy after login
The most recent operation is printed above, and the oldest operation is printed below.
4.2 Eliminate conflicts
When merging operations, conflicts are prone to occur. At this time, you need to- open the editor to resolve the conflict
In actual development, one of them often needs to be deleted, so be sure to- carefully analyze the content of the conflicting part before making modifications.
After resolving the conflict, perform add and commit operations again-
If you are not satisfied with the previous submission information, you can use the amend parameter to modify it
$ git commit --amend
Copy after login
4.3 git rebase -i compress history
Before merging branches, if you find some spelling errors in the submitted content, you may wish to submit a modification, and then include the modification into the previous submission and compress it into a history Record.
git rebase -i HEAD~2
Copy after login
You can select the two latest historical records including HEAD in the branch as objects for the period, and open them in the editor
5 推送至远程仓库
5.1 git remote add 添加远程仓库
在创建新仓库的时候,建议不要勾选 README.md 文件,这样会使本地仓库和远程仓库失去整合性。虽然到时候可以强制覆盖,但防止这一情况发生,还是不要勾选,就创建一个空仓库就好。
git remote 在先写代码,后创建仓库的情况下能较好的使用
$ git remote add origin git@github.com:github-book/git-tutorial.git
Copy after login
对于一般先创仓库,后写代码的,需要先 pull 下来仓库,再对文件进行修改
5.2 git push 推送至远程仓库
推送至 master 分支
$ git push -u origin master
Copy after login
-u 参数可以在推送的同时,将 origin 仓库的 master 分支设置为本地仓库当前分支的 upstream(上游),添加这个参数,将来运行 git pull 命令从远程仓库获取内容的时候,本地仓库的这个分支就可以直接从 origin 的 masteer 分支获取内容,省去了另外添加参数的麻烦
除了 master 分支之外,还可以推送到其他分支
$ git checkout -b feature-D
$ git push -u origin feature-D
Copy after login
6 从远程仓库中获取
6.1 git clone 获取远程仓库
$ git clone git仓库地址
Copy after login
将本地的 feature-D 分支更新到最新状态
$ git pull origin feature-D
Copy after login
- 多名开发者在同一个分支中进行作业时,为减少冲突情况的发生,建议更频繁的进行 push 和 pull 操作
推荐学习:《Git教程》
The above is the detailed content of How to understand the basic use of git in 20 minutes. For more information, please follow other related articles on the PHP Chinese website!