0%

Git 操作备忘

控制

  • git init G
  • git add/rm
  • git commit -m “informatiom”

查看修改

  • git status
  • git diff # failure after use git add
  • git log [–pretty=oneline//不输出作者时间等信息 ]
    • git log --graph --pretty=oneline --abbrev-commit

diff

  • git diff # 输出工作区和暂存区的差异
  • git diff --cached # 输出暂存区和本地最近的版本 (commit) 的差异
  • git diff HEAD # 输出工作区、暂存区 和本地最近的版本 (commit) 的差异
  • git whatchanged --since=‘2 weeks ago’ # 查看两个星期内的修改

版本回退

  • git reset
    • git reset --hard HEAD^ # ^ 表示前一个版本,^^ 为两个,以此类推
    • git reset --hard HEAD~100 # 表示前 100 个版本
    • git reset --hard commit_id
  • git revert HEAD # 撤销当前提交,不需要^
  • git reflog

reset VS revert

  • reset 直接忽略已经提交的 commit, 常用于本地;
  • revert 保留原有的 commit, 创建一个新的节点使其与上一个节点内容相同,多用于已经 push 到远程的提交

撤销修改

让工作区文件(未 add)回到最近一次 git commit 或 git add 时的状态

  • git checkout – file # 单个文件或文件夹
  • git checkout . # 针对所有文件

缓存区

  • git reset HEAD file
    若已经使用 git add 则先清缓存区,再清工作区

删除文件

  • git rm
  • git checkout – file
    用版本库里的版本替换工作区的版本

删除未追踪(untracked)文件

  • git clean -f 强制删除文件
  • git clean -df 同时删除目录
  • git clean -xdf 同时删除被 ignore 的文件

修改提交记录

  • git commit --amend # 修改 commit 的内容
  • git commit --amend --author=‘Author Name email@address.com’ # 同时修改作者

远程

push

  • git remote add origin git@github.com:ViggoC/Test.git
  • git push -u origin master

clone

fetch

  • git fetch --all && git reset --hard origin/master # 抛弃本地所有的修改,回到远程仓库的状态。

git fetch 将远程主机的最新内容拉到本地,用户在检查了以后决定是否合并到工作本机分支中。而git pull 则将远程主机的最新内容拉下来后直接合并,即:git pull = git fetch + git merge,这样可能会产生冲突,需要手动解决。

rebase

  • git rebase # 把本地未 push 的分叉提交历史整理成直线,未 push 的修改都在最新远程状态之后

分支

切换

  • git checkout - # 快速切换到上一个分支

合并

  • git checkout -b dev
    • git branch dev
    • git checkout dev
  • git merge
  • git branch -d dev # 删除分支
    • git branch -D # force delete
  • git merge --no-ff -m “merge with no-ff” dev
    普通合并而非 fast forward,先创建一个新的 commit 后合并
  • git branch -vv # 展示本地分支关联远程仓库的情况

Bug 分支

在 dev 中工作到一半,需要修改 bug101

  • git stash
  • git check master
  • git check -b issue-101
  • debug 后提交
  • git checkout master
  • git merge --no-ff “merge bug fix 101” issue-101
  • git branch -d issue-101
  • git checkout dev
  • git stash pop
    • git stash list
    • git stash apply stash@{}

多人协作

  • 查看远程库信息,使用 git remote -v;
  • 本地新建的分支如果不推送到远程,对其他人就是不可见的;
  • 从本地推送分支,使用 git push origin branch-name,如果推送失败,先用 git pull 抓取远程的新提交;
  • 在本地创建和远程分支对应的分支,使用 git checkout -b branch-name origin/branch-name,本地和远程分支的名称最好一致;
  • 建立本地分支和远程分支的关联,使用 git branch --set-upstream branch-name origin/branch-name;
  • 从远程抓取分支,使用 git pull,如果有冲突,要先处理冲突。

标签

  • git tag [tag-name] [-m “message”] [commit-id]
  • git show
  • git push origin # 可以推送一个本地标签;
  • git push origin --tags # 可以推送全部未推送过的本地标签;
  • git tag -d # 可以删除一个本地标签;
  • git push origin :refs/tags/ # 可以删除一个远程标签,推送“空”到远程

多平台换行符问题

不统一的换行符确实对跨平台的文件交换带来了麻烦。换行符发生改变时,Git 会认为整个文件被修改。

git 的换行符设置有三种模式

1
2
3
4
5
6
7
8
# 提交时转换为 LF,检出时转换为 CRLF,适用于 windows 平台
git config --global core.autocrlf true

# 提交时转换为 LF,检出时不转换
git config --global core.autocrlf input

# 提交检出均不转换
git config --global core.autocrlf false

若在 Windows 下配置 core.autocrlf true 并 pull 文件,并把文件拷贝到配置为 core.autocrlf false 的系统下,提交代码时就会发现每一行都被修改了。

此外,还有一个core.safecrlf用于检查文件是否包含混合换行符,其有三个可选项:

1
2
3
4
5
6
7
8
# 拒绝提交包含混合换行符的文件
git config --global core.safecrlf true

# 允许提交包含混合换行符的文件
git config --global core.safecrlf false

# 提交包含混合换行符的文件时给出警告
git config --global core.safecrlf warn

Windows 上 Git bash 客户端自带 dos2unix 转换工具可以完成换行符的转换。

清空 commit history

  1. Checkout

    git checkout --orphan latest_branch

  2. Add all the files

    git add -A

  3. Commit the changes

    git commit -am "commit message"

  4. Delete the branch

    git branch -D master

  5. Rename the current branch to master

    git branch -m master

  6. Finally, force update your repository

    git push -f origin master

学习资源

git-简明指南

在线学习网站:可实操的教程,通过动画展示版本关系

git tips 思维导图

cheat sheet