Check if git is installed on the computer
git –-version
If it is installed, it will print the version information of git to
create a folder
mkdir <filename>
Enter the folder
cd <filename>
Initialize the warehouse
git init
Create a file
touch <filename>
View the status of the local warehouse
git status
View changes in the local warehouse
git diff #看当前工作树和暂存区的差别
git diff <filename> #看指定文件当前工作树和暂存区的差别
git diff HEAD #查看本次提交和上次提交的差别,这里的 HEAD 是指向当前分支中最新一次提交的指针。
Printed out: + is a new line,-is a deleted line
Restore the last sync state
git checkout . #还原所有文件上一次的同步状态
git checkout <filename> #还原文件 <filename> 上一次的同步状态
Put the code in the staging area
git add . #所有文件放到暂存区
git add <filename> #把文件 <filename> 放到暂存区
Submit the code in the temporary storage area
git commit -m "简要提交信息"
git commit -am "简要提交信息" #等于 git add 加上 git commit
git commit --amend #修改上一次的提交信息
If you want to record the submission information in detail, don't add -m, execute git commit directly, the following interface will appear, press i to edit the
submission remarks can be written as follows:
Line 1: Brief description in one line Submitted change content
Second line: blank line
Third line: describe the reason and details of the change
After editing, press esc to exit editing, enter: wq to save and exit
View the commit log
git log #详细信息,只能查看以当前状态为终点的历史日志
git reflog #查看当前仓库的操作日志,可以在日志中找到哈希值
git log –pretty=short #简要的一行描述信息
git log --graph #以路线图形式查看操作日志
git log --oneline #以简洁单行的形式查看操作日志
git log --oneline --graph #以路线图形式查看操作日志,只显示简要信息
git log --pretty=oneline --graph #跟上面一句的效果一样
git log <filename> #查看指定文件的日志
git log -p #查看文件的改动
git log -p <filename> #查看指定文件的改动
git log –-author='username' #查看指定作者的提交日志
git show <hash> #查看提交的详细内容
If the log is too long, it will display: (colon), press Enter to continue to view, to exit the view, press q
Corrections after submitting errors
git rebase -i HEAD~2 #更正前两次的提交历史
It can be corrected by changing the pick of the final need to fixup
Retrospective version
git reset HEAD <filename> #回溯到上一次指定文件的提交
git reset --hard HEAD^ #回溯到上个版本,^^上上个版本
git reset --hard <hash> #回溯历史版本 hash,hash 的值可以通过 git log 查看
git checkout <hash> <filename> #指定文件的版本回退
Branch view
git branch #显示分支,带有*星号的是当前所在的分支
git branch -a #同时显示本地仓库和远程仓库的分支信息
git branch -av #显示分支信息及提交信息
Branch creation and switching
git branch <branchname> #创建分支 <branchname>
git checkout <branchname> #切换到分支 <branchname>
git checkout - #切换回上一个分支
git checkout -b <branchname> #创建并切换分支。等同于 git branch <branchname> 加上 git checkout <branchname>
Deletion of branch
git branch -d <branchname> #删除分支
git branch -D <branchname> #强制删除分支
git push origin –-delete <name> #删除远程仓库的分支
Merge branch
git merge <branchname> #合并分支
git merge --no-ff <branchname> #加上--no-ff参数,创建合并提交
If there is a conflict, you need to re-git add after resolving it
Local warehouse associated with remote warehouse
git remote add origin <ssh> #ssh是远程仓库的地址
Push code to remote warehouse
git push #推送代码
git push origin <branchname> #推送到远程仓库的指定分支上
git push -u origin <branchname> #推送到远程仓库,-u参数可以在推送的同时将指定分支设置为本地仓库当前分支的upstream
Synchronize remote warehouse code
git pull #拉取最新代码
git pull origin <branchname> #从远程仓库的指定分支拉取最新代码
git checkout -b <branchname> origin/<branchname> #从 origin 仓库里的指定分支拉取下来至本地新建分支
Pull remote warehouse
git fetch #从远程仓库实际获取(fetch)最新源代码,与自己仓库的合并
Clone remote warehouse code
git clone <ssh> #克隆到本地
git clone <ssh> <rename> #克隆并指定命名
Use of labels
git tag #查看标签
git tag <tagName> #创建标签
git tag -d <tagName> #删除标签
git tag <tagName> <hash> #创建标签到指定的提交上
git push origin <tagName> #推送指定标签的代码到远程
The tag is added by default on the latest submission
File operations in local warehouses
git rm <filename> #删除文件
git mv <oldFilename> <newFilename> #文件重命名
git mv <filename> <path/filename> #移动文件
git mv <filename> <path/newFilename> #移动并重命名
Books: "GitHub Introduction and Practice"