Please give up the foolish operation of git in the IDE, and get familiar with the commonly used git commands by stepping and changing the scene, and then you can't put it down.
Scenario 1 Upload code to a remote warehouse
# 初始化git仓库
git init
# 全部写入暂存区
git add .
# 将暂存区内容添加到本地仓库
git commit -m "first commit"
# 关联远程仓库
git remote add origin https://github.com/username/*.git
# 提交到远程仓库
git push -u origin master
After the above operations are completed, the code is successfully uploaded to the remote warehouse.
Scenario 2 View operation records
# 查看工作区和和暂存区的文件状态
git status
# 查看全部的提交记录
git log
# 查看某人的提交记录
git log --author='username'
# 查看文件提交记录和commit id
git log --pretty=oneline 目录/文件名
# 查看某次提交的操作记录
git show <commit id>
# 查看文件操作记录和文件变化
git log -p 目录/文件名
# 查看简写的提交记录
git log --oneline
# 查看版本路线
git log --oneline --graph
git log
more parameters of the 0607bee12032f7 command, please refer to the official documentation.
Scenario 3 Configure user name and mailbox
# 配置用户名
git config --global user.name '<username>'
# 为当前仓库配置用户名
git config --add --local user.name '<username>'
# 配置邮箱
git config --global user.email '<email>'
# 为当前仓库配置邮箱
git config --add --local user.email '<email>'
# 查看配置,重点关注user.name和user.email变量的值
git config --global --list
git config
more configuration of the 0607bee120337d command, please refer to the official documentation.
Scenario 4 modify file
git add 文件名
git add 文件名1 文件名2 ...
git add .
git commit -m "修改文件"
git add
command can write a single specific file, multiple specific files, or all modified files into the temporary storage area, and then perform subsequent submission operations.
Scenario 5 delete files
You can delete files manually, but you can also use git commands to delete files.
git rm 文件名
Scene 6 Move and rename files
You can manually move and rename the file, but you need to git add
and git rm
make the status of the file change to renamed
. A more convenient and efficient solution is to use git mv
.
git mv 原文件名 新目录/新文件名
Scenario 7 Restore after file operation
In the state that has not been submitted to the temporary storage area, you can git diff
and manually restore it. The efficiency is too low. It is recommended to use git checkout
.
# 未提交至暂存区的状态下,回到上一次提交的状态
git checkout -- 目录/文件名
# 撤销追踪,回到上一次提交的状态
git reset HEAD 目录/文件名
git checkout -- 目录/文件名
Scene 8 Back to the specified version
# 回到上一个版本,一个^代表回退一个版本
git reset --hard HEAD^
# 回到指定版本
git reset --hard <commit id>
# 指定文件回到指定版本
git checkout <commit id> -- 文件名
Scenario 9 version label management
The newly created label will be added to the most recent submission record by default.
# 创建标签
git tag <tag name>
# 查看标签
git tag
# 为指定的提交记录创建标签
git tag <tag name> <commit id>
# 删除标签
git tag -d <tag name>
# 推送某个标签
git push origin <tag name>
# 推送所有标签
git push origin --tags
Scenario 10 branch management
# 创建分支
git branch <branch name>
# 查看分支
git branch
# 同时查看远程分支及版本
git branch -av
# 切换分支
git checkout <branch name>
# 删除分支
git branch -d <branch name>
# 强制删除分支
git branch -D <branch name>
# 关联远端分支
git branch –set-upstream <branch name> <remote branch name>
# 创建并切换分支,也可关联远端分支
git checkout -b <branch name> [remote branch name]
# 合并分支
git merge <branch name>
# 合并时如果有冲突保留当前分支的代码
git merge --abort
# 拉取分支
git fetch
# 删除远程分支
git push origin --delete <branch name>
# 拉取合并
git pull
For more branch management operations, please refer to the official documentation
Best Practices
Alias
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
# 取消暂存文件
# git reset HEAD -- <文件名>
git config --global alias.unstage 'reset HEAD --'
# 查看最后一次提交
git config --global alias.last 'log -1 HEAD'
# 打开gitk
git config --global alias.visual '!gitk'
rebase
The modification in 0607bee12036a9
C3
. "title=" C4
the modification in C3
to 0607bee12036af. ">
git checkout experiment
git rebase master
Fast forward merge of 0607bee12036eb branch. "title=" Fast forward merge of
master
">
git checkout master
git merge experiment
cherry-pick
Transfer submission
git cherry-pick <commit id>
stash
# 保存当前进度,会分别对暂存区和工作区的状态进行保存
git stash
# git stash带上message
git stash save '<message>'
# 显示已保存的进度列表
git stash list
# 恢复最新保存的进度,并将恢复的进度删除
# 如果提供--index 除了恢复工作区的文件外,还尝试恢复暂存区
# 如果提供stash id则恢复该进度
git stash pop [--index] [stash id]
# 除了不删除恢复的进度之外,其余和 git stash pop 命令一样
git stash apply [--index] [stash id]
# 删除进度
git stash drop [stash id]
# 删除所有存储的进度
git stash clear
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。