1. 初始化基础操作
git init #在当前目录新建一个git代码库
git init <project-name> #新建一个目录,并将其初始化为git代码库
git clone [url] #clone代码
2. 增加/删除
git add [file1] [file2]... #添加指定文件到暂存区
git add [dir] #添加指定目录到暂存区,包含子目录
git add . #将所有的文件都添加到暂存区
git add -p #添加前需要对每一个变化进行确认
git rm [file1] [file2]... #删除工作区文件,并且将这次删除放入暂存区
git rm --cached [file] #停止追踪指定文件,单文件会保留在工作区
3. 代码提交
git commit -m "描述" #提交暂存区到仓库区
git commit [file1] [ifle2]... -m "描述" #提交暂存区的指定文件到仓库区
git commit -a #提交工作区子自次commit之后的变化,直接到仓库区
git commit -v #提交时显示所有的diff信息
4. 分支操作
git branch #列出本地所有分支
git branch -r #列出远端所有分支
git branch -a #列出本地和远端所有分支
git branch <new-branch-name> #新建一个分支,但是依旧停留在当前分支
git checkout -b <branch-name> #新建一个分支,并切换到该分支
git branch <branch-name> <commit-id> #新建一个分支,指向指定commit
git checkout - #切换到上一个分支
git branch --set-upstream-to=origin/<branch-name> #将当前分支和指定远端分支进行关联
#删除分支操作
git branch -d <branch-name> #删除本地分支
git push origin --delete <branch-name> #删除远端分支
5. 标签
git tag #列出所有的tag
git tag <tag-name> #创建一个新的tag
git tag <tag-name> <commit-id> #在指定的提交上创建tag
#添加tag
git tag -a <tag-name> -m <tag-description>
#删除操作
git tag -d <tag-name> #删除指定的本地tag
git push origin :refs/tags/<tag-name> #删除远端tag
git push [remote] [tag] #提交指定tag
git push [remote] --tags #提交所有的tag
git checkout -b <branch-name> <tag-name> #新建一个分支,并指向指定的tag
6. 日志
git log #查看日志
git log <branch-name> #查看指定分支日志
git log --oneline #精简的日志输出
git log --graph #以分支的可视化图显示
撤销日志
git reflog
用处:有时git log 命令无法捕捉到撤销的命令,特别是对于那些无法在commit历史里显示的命令。
当我们在运行类似git rebase 这样危害的命令之后,reflog基本上可以算是一层安全网。不仅可以看到之前所做的commit,而且还可以看到导致commit的每一个过程。
7. 查看当前状态 + 合并冲突
git status #查看当前分支状态
8. 对比差异
git diff --staged #staged的改变
git diff #unstaged的改变
git diff branch1.branch2 #对比两个分支之间的差异
9. 导航
git reset <commit-id>
该命令将会撤销对应的commit,并且取消那次commit中的stage操作,但是文件仍然保留在工作目录中。
10. 切换分支
git switch branch-name #切换到指定的分支(Git 2.23中的新语法)
git checkout branch-name #切换到指定的分支(经典分支)
git restore #用来checkout文件
git switch #用来checkout分支
git checkout -b <branch-name> #创建新的分支并切换过去
#将远程git仓库里的指定分支拉取到本地(本地不存在的分支)
git checkout -b 本地分支名 origin/远程分支名
11. 修改
git reset --hard HEAD #重置本地目录到最近一次commit的状态,并且会放弃所有unstage的文件
git restore <filename> #将一个文件重置到之前的提交(Git 2.23 语法)
git checkout --<filename> #讲一个文件重置到之前的提交(经典语法)
git reset --hard HEAD~1 #撤销上一次commit并重写历史
git reset --hard HEAD~N #回到N次commit之前
git reset --hard <commit-id> #回到特定的指定的提交
soft、mixed和hard三种reset的不同
--soft:撤销commit但是工作中
--mixed:(默认)撤销commit,撤销当次commit的stage,但是工作目录中会保留更改
--hard:撤销commit,撤销当次commit的stage,并且删除更改
12. 推送/拉取
git push #推送提交的代码
git push origin branch-name #推送到指定的远端分支
git pull #拉取远端代码
git pull origin branch-name #从指定的远端分支拉取代码
13. 合并
git merge <branch-name> #合并指定分支到当前分支
git merge --no-f -m "提交描述" <branch-name> #创建一个带有单独提交的merge
git cherry-pick <commit-id> #将commit-id的提交合并到当前分支
git checkout <branch-name> <filename> #将指定分支的指定文件拉取到当前的分支
14. 停止追踪某个文件
git rm --cached <filename> #该分支中停止对改文件的追踪
15. 暂存命令
git stash #暂存当前的变更文件
git stash save "暂存描述" #暂存并填写描述
git stash -u #同时也暂存未被追踪的文件
查看stash的列表
git stash list #查看stash中的暂存列表
将stash中的文件取出
git stash pop #将最近添加的stash栈弹出
git stash apply stash@{stash_index} #将第stash_index个stash栈弹出
16. 撤销commit而不重写历史
git revert HEAD #撤销最近一次的commit
git revert <commit-id> #撤销指定提交的commit
这条命令将重新运行提交新的commit时的逆过程,从而撤销你的更改而不会撤销历史记录,在共享的分支中撤销commit时,重写历史记录会非常的复杂,所以使用git rever是一种很安全的解决方式。