git 常见场景及命令

228 阅读1分钟

1. 从远程仓库某一分支拉取代码

git clone -b branch-name remote-path

2. 创建分支

git branch branch-name
  • 切换分支
git checkout branch-name
  • 创建 + 切换
git checkout -b branch-name
  • 删除分支
git branch -d branch-name

3. 提交到暂存区(任何修改都要先提交到暂存区)

git add file-name
git add

4. 提交本地版本库 commit

git commit -m "xxx"

5. 把某一分支合并到当前分支

git merge branch-name

6. 推送到远程仓库

git push origin branch-name

7. 暂存当前工作内容,创建修复bug分支

  • 暂存工作内容
git stash
  • 恢复工作内容
git stash apply
  • 删除暂存记录
git stash drop
  • 恢复 + 删除
git stash pop
  • 查看暂存列表
git stash list

8. 查看信息

  • 查看工作区状态
git status
  • 常看分支
git branch -av
  • 查看记录
git log
  • 查看配置
git config --list

9. 错误回退

  • 版本回退
git reset ^

git reset ^10
git reset commit-id

  • 场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file。

  • 场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD ,就回到了场景1,第二步按场景1操作。

  • 场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,参考版本回退一节,不过前提是没有推送到远程库


10. 重命名

git log
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"