Home >Development Tools >git >How to understand the basic use of git in 20 minutes
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.
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"
"your_email@example.com"
Set it as the registered email address of your GitHub account$ cat ~/.ssh/id_rsa.pub
ssh-rsa The content of the public key your_email@example.com
Also copy it
$ 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.
$ git clone git@github.com:hirocastest/Hello-World.git
$ git add 文件夹/文件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 "记录一行提交信息"
push command, and the warehouse on GitHub will be updated
$ git push2.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 init2.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)
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 +0800If 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=short2.8 git diff Check the difference before and after the changeExecute
git diff Check the difference between the current working book and the staging area
$ git diff
$ git branch * master3.2 git checkout creates and switches branchesCreate and switch to branch feature-A
$ git checkout -b feature-AIn fact, the above command is equivalent to the following two commands
$ git branch feature-A $ git checkout feature-ASwitch back to branch main
$ git checkout mainSwitch back to the previous branch
$ git checkout -
$ git merge --no-ff featureThe editor will then start to enter the merge submission information
3.4 git log --graph View branches as icons
$ git log --graph
$ git reset --hard 目标时间点的hash值
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 reflogThe most recent operation is printed above, and the oldest operation is printed below.4.2 Eliminate conflicts
$ git commit --amend4.3 git rebase -i compress historyBefore 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~2You can select the two latest historical records including HEAD in the branch as objects for the period, and open them in the editor
在创建新仓库的时候,建议不要勾选 README.md 文件,这样会使本地仓库和远程仓库失去整合性。虽然到时候可以强制覆盖,但防止这一情况发生,还是不要勾选,就创建一个空仓库就好。
git remote 在先写代码,后创建仓库的情况下能较好的使用
$ git remote add origin git@github.com:github-book/git-tutorial.git
对于一般先创仓库,后写代码的,需要先 pull 下来仓库,再对文件进行修改
推送至 master 分支
$ git push -u origin master
-u 参数可以在推送的同时,将 origin 仓库的 master 分支设置为本地仓库当前分支的 upstream(上游),添加这个参数,将来运行 git pull 命令从远程仓库获取内容的时候,本地仓库的这个分支就可以直接从 origin 的 masteer 分支获取内容,省去了另外添加参数的麻烦
除了 master 分支之外,还可以推送到其他分支
$ git checkout -b feature-D $ git push -u origin feature-D
$ git clone git仓库地址
将本地的 feature-D 分支更新到最新状态
$ git pull origin feature-D
推荐学习:《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!