Git -- 常见使用指令

190 阅读1分钟

常见指令

  1. 克隆: git clone 仓库地址
  2. 初始化: git init
  3. 查看远程仓库: git remote -v
  4. 拉取远程仓库: git pull [remoteName] [localBranchName]
  5. 推送远程仓库: git push [remoteName] [localBranchName]
  6. 暂存: git add .
  7. 提交: git commit -m "说明"
  8. 查看分支: git branch
  9. 切换分支: git checkout [name]
  10. 合并分支: git merge [name]

实用指令

  1. 修改最近一次提交:

​ 方法一: commit --amend

git add . 
git commit --amend
git push <remote> <branch> -f

​ 方法二: 用 reset 后修改

git reset HEAD^
git add .
git commit -m "new commit message"
git push <remote> <branch> -f

​ 方法三: 分支提交错误

# 取消最新的提交,储藏更改
git reset HEAD~ --soft
git stash
# 切换正确分支,应用储藏
git checkout name-of-the-correct-branch
git stash pop
git add .
git commit -m "commit message"
git push <remote> <branch> -f
  1. 修改第 n 次提交
# 1. 查看修改
# 查看最近一次
git rebase -i master~1
# 查看最近五次
git rebase -i master~5
# 当前版本的倒数第 2 次
git rebase -i HEAD~2
# 指定 SHA 位置
git rebase -i 33eea88a

# 修改
git add .
git commit --amend
# 完成操作
git rebase --continue
git push <remote> <branch> -f
  1. 推送到 Gerrit 仓库
git push origin HEAD:refs/for/name-of-the-target-branch