Git 常用命令

仓库

  • git clone
  • git remote -v 查看远程仓库
  • git pull [远程分支] [本地分支] 拉取远程分支到本地分支,如果省略本地,则将合并到当前分支
  • git push 推送到仓库

分支

  • 查看远程分支: git branch -r
  • 列出所有分支: git branch -a
  • 创建分支: git branch [name]
  • 创建并切换分支: git checkout -b [name]
  • 删除分支: git branch -d [name] -D 强制删除
  • 合并分支: git merge [name] 将[name]分支与当前分支合并
  • 创建远程分支(本地分支push到远程): git push origin [name]
  • 删除远程分支: git push orgin :[name] / git push origin --deleted [name]
  • 推送分支: git push origin [localName]:[remoteName]

代码提交

  • 查看文件修改记录: git status
  • 查看修改详情: git diff
  • 添加到缓存: git add . . 表示全部添加
  • 提交注释: git commit -m '注释'
  • 提交代码: git push

版本回退

  • 查看日志: git log / git reflog
  • 回退到上一个版本: git reset --hard HEAD^ [file]
  • 向前回退第n个版本: git reset --soft HEAD~n
  • 将本地回退到与远程一样: git reset --hard origin/master
  • 回退到某个版本: git reset [版本号] / git reset --hard [版本号]
  • 回退到上一次提交: git revert HEAD
  • 撤销merge,先回退版本,再push: git push --force

放弃本地修改

  1. 未使用git add 缓存代码时

    • git checkout --filepathname
    • git checkout . 放弃所有修改
  2. 已使用git add 缓存代码时

    • git reset HEAD filepathname
    • git reset HEAD . 放弃所有缓存
  3. 已使用git commit 提交了代码

    • git reset --hard HEAD^ 回退上一次commit
    • git reset --hard [版本号] 回退到任意版本
原文地址:https://www.cnblogs.com/lshilin/p/10238754.html