常用 Git 命令清单

前言

知识的四大阶段,记录,分享,讨论

常用命令

重要数据流:

  1. 数据流一: Workspace (add) -> Index (commit)-> Respository (push)-> Remote
  2. 数据流二:Remote (fetch/clone) -> Respository (checkout) -> workspace
  3. 数据流三:Remote (pull) -> workspace

名词解释:

  1. Workspace : 工作区
  2. Index / Stage : 暂存区
  3. Respository: 仓库区(或本地仓库)
  4. Remote: 远程仓库

入门

新建代码库 和配置

# 新建一个目录,将其初始化为Git代码库
1. $git init [project-name]:  

# 显示当前的Git 配置,
# 配置文件为 .gitconfig
2. $git config --list

增删提交代码

# 查看代码修改情况
1. $git status

# 添加代码到暂存区, 全添加
2. $git add . 

# 提交暂存区代码到仓库,全部提交,注释信息
3. $git commit -a -m [message]

# 本地分支推送本地仓库代码到与之关联的远程仓库
4. $git push origin

# 本地分支拉取与之关联的远程仓库代码
5. $git pull origin

# 查看当前分支的版本历史
6. $git log

# 显示暂存区和工作区的差异
7. $git diff

进阶

分支

# 列出所有分支
1. $git branch

# 新建分支
2. $git branch [branch-name] 

# 新建分支,并切换到该分支
3. $git checkout -b [branch-name]

# 切换到指定分支,并更新工作区
4. $git checkout [branch-name]

# 合并指定分支到当前分支
5. $git merge [branch]

# 删除分支
6. $git branch -d [branch-name]

# 提交暂存区代码到仓库,全部提交,注释信息
3. $git commit -a -m [message]

标签

# 列出所有 tag
1. $git tag

# 在当前的 commit 上新建一个 tag 
2. $git tag [tag-name]

# 删除 tag
6. $git tag -d [tag-name]

远程同步

# 显示所有远程仓库
1. $git remote -v

# 显示某个远程仓库的信息
2. $git remote show [remote]

# 获取远程所有变动
3. $git fetch 

# 获取指定远程仓库的所有变动
3. $git fetch [remote]

撤销

# 重置暂存区和工作区,与上一次 commit 保持一致
1. $ git reset --hard

# 重置当前分支的HEAD 为指定commit ,同时重置暂存区和工作区,与指定commit一致
2. $ git reset --hard [commit]

# 融合
3. $git merge [branch]

# 衍合
4. $git rebase [branch]

致谢

http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html

posted @ 2018-04-11 11:04  但,我知道  阅读(202)  评论(0编辑  收藏  举报