Git 简明手册

1. 代码仓库及工作区:

    1. 工作区 working directory
    1. 暂存区 staging area
    1. 仓库区 repository

查看远程仓库

git remote -v

添加远程仓库

git remote add <repo_name> <git_url>

删除远程仓库

git remote rm <repo_name>

重命名远程仓库

git remote rename <old_repo_name> <new_repo_name>

2. 代码从拉到推过程

git fetch origin <branch>
git merge <branch>
git add xxx
git commit -m "xxx"
git push origin <branch>

3. 替换本地改动

撤销工作区修改

git checkout -- <filepath>

撤销暂存区修改

git reset HEAD <filepath>

用远程仓库中的代码覆盖本地代码

git fetch origin <branchname>

整个工作区用远程仓库的代码分支代替

git reset --hard origin/<branchname>
git reset --hard HEAD

单个文件用远程文件代替

git checkout origin/master --  platform/src/utiltp/CmTraceFromT120.cpp
git checkout HEAD -- src/evt/CXmlDSEvent.cpp

4. 创建切换删除分支

  1. 创建分支

    git checkout -b <branchname>

  2. 切换分支

    git checkout <branchname>

  3. 删除分支

    git branch -d <branchname>

删除远程分支:

git push origin --delete <branchname>
  or git push origin :<branchname>

清空本地分支:

git remote prune origin
  1. 查看分支

    git branch -a

  2. 重命名分支

    git branch -m <old_branch_name> <new_branch_name>
    git push origin <new_branch_name>

5. 查看历史

显示最近5次的提交的概要历史记录

git log --graph --stat --oneline -5

显示最近3次的提交的详细历史记录

git log -p -3

6. 比较区别

1) 比较工作区和暂存区 git diff
2) 比较暂存区和仓库区 git diff --cached
3) 比较工作区和仓库区 git diff HEAD

git diff ref1:path/to/file1 ref2:path/to/file2

一般来说, ref1 and ref2 可以是分支名, 也可是仓库名/分支名, 或者 commit 哈希

6.1 比较远程文件与本地文件

git diff remotename/branchname:remote/path/file1.txt local/path/file1.txt

6.2 比较两个分支中的文件

git diff HEAD:local/path/file1.txt remotename/branchname:remote/path/file1.txt

7. 管理标签

7.1 显示标签

git tag

7.2 添加标签

git tag -a <tagname> -m <comments>

7.3 推送标签

git push --tags

7.4 获取远程标签

git fetch origin tag <tagname>

7.5 删除标签

git tag -d <tagname>
git push origin :refs/tags/<tagname>
git push origin --delete tag <tagname>

8. 子模块

添加子模块

git submodule addhttps://chromium.googlesource.com/v8/v8.git

初始化子模块

git submodule init

修改子模块

git submodule update

9. 回退

git log -10
git reset xxx
git push origin <branchname> --force

10. 冲突解决

git stash
git stash pop

Reference

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 以下内容是我在学习和研究Git时,对Git操作的特性、重点和注意事项的提取、精练和总结,可以做为Git操作的字典,...
    科研者阅读 3,577评论 2 19
  • git作为时下最流行的代码管理工具,Git权威指南总结了十条喜欢Git的理由: 异地协同工作; 现场版本控制; 重...
    古斟布衣阅读 1,850评论 0 12
  • 以下内容是我在学习和研究Git时,对Git操作的特性、重点和注意事项的提取、精练和总结,可以做为Git操作的字典,...
    科研者阅读 4,249评论 4 50
  • 我在梦里找小姐,但我没做,我花了一夜的时间来劝小姐从良,由此可见我内心的纯净,我当时还没开窍,在我的潜意识里,说...
    TOKIWA阅读 203评论 0 0
  • 每个周六的晨会都能让我从各位同学的分享中了解学到更多的新知识,增长见闻,通过对比发现自己的不足之处,从而给...
    男儿当自强不息阅读 304评论 3 2