Git相关命令汇总

Git命令庞大繁杂,有必要做个备份以供参考。这里仅列出最基础的部分命令。
先上个图感受下

  • 合并本地分支

    git merge local_branch_name

  • 合并远程仓库

    git merge origin/remote_branch_name

  • 查看当前分支与远程分支的对应关系

    git branch -vv

  • 列举所有配置

    git config -l

  • 比较暂存区和版本库差异

    git diff --cached

  • 将工作区做的修改暂存到一个git栈中
    git stash

  • 查看栈中所有暂存

    git stash list

  • 将暂存栈中指定编号恢复到工作区

    git stash apply stash_number (stash_numer:暂存编号 注意:暂存还在栈中)

  • 将栈顶的暂存恢复到工作区

    git stash pop

  • 清空暂存栈

    git stash clear

  • 查看远程服务器仓库状态

    git remote show origin

  • 删除远程仓库地址

    git remote rm

  • 修改远程地址

    git remote set-url origin git@github.com:robbin/robbin

  • 添加远程仓库地址

    git remote add origin git@github:robbin/robbin_site.git

  • 查看当前没有被合并的分支

    git branch --no-merged

  • 创建远程分支

    git push origin local_branch_name:remote_branch_name

  • 创建本地分支与远程分支关联(当前没有创建对应的本地分支)

    git checkout --track origin/remote_branch_name

    git checkout -b local_branch_name origin/remote_branch_name

  • 将本地分支与远程分支关联(已创建本地分支)

    git branch --set-upstream-to=origin/remote_branch_name

    git branch -u origin/remote_branch_name

  • 修改上一次的提交信息

    git commit --amend

  • 修改历史提交信息

    git rebase -i HEAD~20

  • 取消暂存

    git reset HEAD file_name

  • 撤销对文件的修改

    git checkout --file_name

  • 本地代码库回滚

    git reset --hard commit_id (回滚到commit-id,将commit-id之后提交的commit都去除)

    git reset --hard HEAD~3 (将最近3次的提交回滚)

  • 远程代码库回滚

    应用场景:系统发布后发现问题,需要回滚到某一个commit-id,再重新发布

    原理:先将本地分支退回到某个commit-id,删除远程分支,再重新push本地分支

    操作步骤

    1. git checkout the_branch

    2. git pull

    3. git checkout the_branch_backup (备份一下这个分支当前的情况)

    4. git reset --hard the_commit_id (把the_branch本地回滚到the_commit_id)

    5. git push origin :the_branch (删除远程the_banch分支)

    6. git push origin the_branch (用回滚后的本地分支重新建立远程分支)

    7. git push origin :the_branch_backup (如果前面成功了,删除这个备份分支)


如有任何错误之处或疑问,请联系我:fishnewsdream@gmail.com,欢迎交流,共同提高!

Objective-C/Swift技术开发交流群201556264,讨论何种技术并不受限,欢迎各位大牛百家争鸣!

微信公众号OldDriverWeekly,欢迎关注并提出宝贵意见

老司机iOS周报,欢迎关注或订阅

刚刚在线工作室,欢迎关注或提出建设性意见!

刚刚在线论坛, 欢迎踊跃提问或解答!

如有转载,请注明出处,谢谢!

本站总访问量 本文总阅读量