git常用指令

104 阅读1分钟
git pull  # 拉取远程分支代码

git push  # 推送代码到远程分支

git add [fileName]  # 添加工作区指定文件至暂存区
git add . # 添加工作区所有文件至暂存区

git commit -m [msg]  # 将暂存区文件添加到本地仓库
git commit -am [msg] # git add .和git commit的合写
git commit --amend 
# 在弹出面板输入i进入编辑模式,修改commit msg信息,按esc退出编辑,输入wq退出完成修改

git checkout [brancnName]  # 切换到指定分支
git checkout -b [branchName]  # 从当前分支创建新分支并切换到创建的新分支上


git merge [brachName]  # 将指定分支合并到当前分支,并自动提交
git merge --continue  # 解决冲突后合并
git merge --abort  # 退出冲突合并
# 推荐使用rebase替代merge,因为merge会产生新的提交记录,rebase不会产生新的提交记录
git rebase [branchName]  # 将指定分支合并到当前分支,并自动提交
git rebase --continue
git rebase --abort

# 撤销操作
git branch [fileName]  # 将工作区指定文件撤销
git branch .  # 将工作区所有文件撤销
git restore --staged [fileName]  # 将暂存区指定文件撤回工作区
git restore --staged .  # 将暂存区所有文件撤回工作区
git reset --soft HEAD~1  # 撤销最近一次的commit
git reset --mixed HEAD~1 # 撤销最近一次的commit和add
git reset --hard HEAD~1 撤销最近一次的commit和add,并执行git branch .(即回到上一次commit状态)

# 储藏
git stash # 储藏工作区所有更改
git stash save [tag] # 储藏更改并提供tag标识
git stash pop # 从储藏栈中弹出最新储藏并删除
git stash apply # 应用最新储藏(但是不删除)
# 分支版本回退 TODO