Git 常用命令

140 阅读4分钟

git config

git config --global user.name your_name
git config --global user.email mail@address
git config --system core.editor vim

git clone

git clone <repo>

git clone --depth=1 <repo>
git fetch --depth=10
git fetch --unshallow

# https://git-scm.com/book/en/v2/Git-Tools-Submodules
git submodule init
git submodule update

git pull

git fetch
git fetch origin <branch>  # 仅拉取指定的远程分支

git pull  # 从默认远程仓库拉取代码,并合并到当前本地分支
git pull --rebase  # 从默认远程仓库拉取代码,并rebase当前本地分支

git push

git push  # 从当前本地分支推送代码到远程仓库
git push origin <branch>  # 创建新的远程分支
git push --force  # 强制把当前本地分支更新到远程分支,会破坏远程分支的历史记录,不安全
git push --force-with-lease  # 强制把当前本地分支更新到远程分支,但不能改写历史记录

git commit

git status  # 查看当前工作区状态,包括所在分支以及修改的文件
git add <path> # 把工作区的文件放入暂存区
git rm <path>  # 从工作区和暂存区中删除文件
git mv <path1> <path2>  # 从工作区和暂存区中移动文件

git commit  # 提交代码,创建新的commit
git commit --amend  # 提交代码,更新最新的commit而不创建新的commit

git stash

# https://git-scm.com/book/en/v2/Git-Tools-Stashing-and-Cleaning
git stash  # 把工作区的修改贮藏起来,工作区和暂存区的内容重置到HEAD
git stash list  # 查看贮藏区列表
git stash pop  # 把贮藏区最新的修改移出并应用到当前工作区
git stash pop stash@{<n>}  # 把贮藏区第n个的修改移出并应用到当前工作区
git stash drop  # 放弃最近贮藏的修改
git stash drop stash@{<n>}  # 放弃贮藏区第n个的修改
git stash apply stash@{<n>}  # 把贮藏区第n个的修改应用到当前工作区

git revert

git revert <commit>  # 创建一个新的commit来回滚指定commit中的修改

git clean -fd <path>  # 删除工作区中不在暂存区中的所有文件和目录

git reset -- <path>  # 从暂存区中删除指定文件,但不修改工作区内容
git reset <commit>  # 把HEAD切换到commit并更新暂存区,但保持工作区的内容不变
git reset <commit> -- <path>  # 把暂存区中的指定路径更新为指定commit中的内容
git reset --soft <commit>  # 保持工作区和暂存区的内容不变并切换HEAD到COMMIT
git reset --hard <commit>  # 同时更新工作区和暂存区的内容并切换HEAD到COMMIT

git diff

git diff  # 查看工作区与暂存区的diff
git diff -- <path>  # 查看跟指定路径有关的diff
git diff --cached  # 查看暂存区跟HEAD的diff
git diff <commit>  # 查看工作区跟指定commit的diff
git diff <commit> <commit>  # 查看两个commit间的diff
git diff HEAD  # 查看工作区跟最新commit的diff

git log

git log  # 查看当前分支的所有历史记录
git log --oneline
git log -- <path>  # 显示跟指定路径有关的历史记录

git reflog  # 查看HEAD引用的历史记录
git reflog <ref>  # 查看指定引用或分支引用的历史记录
git show <commit>  # 查看commit信息 

git checkout

git branch      # 查看所有本地分支
git branch -vv  # 查看分支的详细信息
git branch -D <branch>  # 删除本地分支
git branch -u origin <branch>  # 设置本地分支的upstream,也就是从哪个远程分支拉取和推送代码

git switch <branch>     # 切换本地分支,如果分支名字跟远程分支匹配则从远程分支创建本地分支
git switch -c <branch>  # 创建新的本地分支

git merge <branch>  # 把指定分支合并到当前分支
git merge-base <branch1> <branch2>  # 查看分支或者commit的最近公共祖先

git checkout <commit>   # 切换HEAD到commit
git checkout -- <path>  # 把工作区的指定路径更新为暂存区的内容
git checkout <commit> -- <path>  # 把工作区和暂存区的指定路径更新为指定commit的内容

git rebase

git rebase <upstream>  # 把当前分支从<upstream>后的修改rebase到<upstream>上
git rebase -i <upstream>  # 交互式的rebase
git rebase --onto <newbase>  # 把当前分支从当前upstream后的修改rebase到<newbase>上
git rebase --onto <newbase> <upstream>  # 把当前分支从<upstream>后的修改rebase到<newbase>上

比较差异的方法

# https://git-scm.com/docs/git-diff
# https://www.jianshu.com/p/bb97fabb475e

# 1.显示出branch1和branch2中差异的部分
git diff branch1 branch2 --stat

# 2.显示指定文件的详细差异
git diff branch1 branch2 <具体文件路径>

# 3. 显示出所有有差异的文件的详细差异
git diff branch1 branch2

# 4. 查看branch1分支有,而branch2中没有的log
git log branch1 ^branch2

# 5. 查看branch2中比branch1中多提交了哪些内容
git log branch1..branch2
# 注意,列出来的是两个点后边(此处即dev)多提交的内容。

# 6. 不知道谁提交的多谁提交的少,单纯想知道有什么不一样
git log branch1...branch2

# 7. 在上述情况下,在显示出每个提交是在哪个分支上
git log --lefg-right branch1...branch2
# 注意 commit 后面的箭头,根据在 --left-right branch1...branch2 的顺序
# 左箭头 < 表示是 branch1 的,右箭头 > 表示是branch2的