stash 贮藏修改

  1. # 查看当前的文件状态
  2. git status
  3. # 结果,有很多文件没有提交
  4. On branch develop
  5. Your branch is up-to-date with 'origin/develop'.
  6. Changes not staged for commit:
  7. (use "git add <file>..." to update what will be committed)
  8. (use "git checkout -- <file>..." to discard changes in working directory)
  9. # 使用 git stash 进行工作区存储
  10. git stash
  11. # 结果
  12. Saved working directory and index state WIP on develop: 27c133b ***
  13. HEAD is now at 27c133b ***
  14. # 查看工作区状态
  15. git status
  16. # 结果
  17. On branch develop
  18. Your branch is up-to-date with 'origin/develop'.
  19. nothing to commit, working tree clean
  20. # 查看分支情况
  21. git branch -vv
  22. # 结果
  23. * develop 27c133b [origin/develop] [*]网民观点没有 title
  24. # 创建一个本地分支
  25. git checkout -b feature/vendor
  26. # 结果
  27. Switched to a new branch 'feature/vendor'
  28. # 查看工作区状态
  29. git status
  30. # 结果
  31. On branch feature/vendor
  32. nothing to commit, working tree clean
  33. # 将存储到stash里面的状态取回来
  34. git stash list
  35. git stash pop
  36. # 结果
  37. On branch feature/vendor
  38. Changes not staged for commit:
  39. (use "git add <file>..." to update what will be committed)
  40. (use "git checkout -- <file>..." to discard changes in working directory)
  41. no changes added to commit (use "git add" and/or "git commit -a")
  42. Dropped refs/stash@{0} (6c816ad6231efc5b593f5c5db76e57226d834c44)
  43. # 推送到远端
  44. git push origin feature/vendor:feature/vendor
  45. bash

其他分支的修改合回 develop

  1. # 当前分支提交,develop分支拉最新的代码
  2. git add .
  3. git commit -m 'commit branck'
  4. git checkout develop
  5. git pull
  6. git checkout feature/work
  7. # 将develop分支的内容合到 feature/work
  8. git merge develop
  9. # 提交这次合并
  10. git commit -m '将 develop 合并到 feature/work'
  11. # 如果有冲突,就修改冲突文件,然后 git add 冲突文件名字
  12. # 切换到 develop ,将 feature/vendor 的改动合进来
  13. git checkout develop
  14. git merge feature/venodr
  15. bash

迁入一个远程分支到本地

  1. git checkout -b master origin/master

撤销一次未提交的远程的 commit

  1. # 查看提交记录
  2. git log
  3. # commit d306b98be7883aabe267ee6940e8d7ee40090c90
  4. # ...
  5. # 回到最后一次提交的上一次
  6. git reset --hard d306b98be7883aabe267ee6940e8d7ee40090c90
  7. bash

删除不存在对应远程分支的本地分支

  1. git fetch -p

当前分支与远程分支链接

  1. # 查看链接到远程分支的情况
  2. git branch -vv
  3. # 断开当前链接与远程的链接
  4. git branch --unset-upstream
  5. # 当前分支链接远程分支
  6. git branch --set-upstream-to=origin
  7. bash

为当前状态打 tag

查看分支下所有 tag

  1. git tag

打标签

  1. git tag v0.7.0

给指定 commit 打标签

  1. git log # 查看 commit
  2. git tag -a v0.7.0 9fbc3d0 # 给指定 commit 打标签

删除标签

  1. git tag -d v0.7.0

将标签推送到远程

  1. git push origin v0.7.0 # 将 v0.7.0 标签提交到 git 服务器
  2. git push origin --tags # 将本地所有标签一次性提交到git服务器

删除远程 tag

  1. git tag -d <tagname>
  2. git push origin :refs/tags/<tagname>

拉取远程 tag

  1. git fetch origin --tags

总结

配置个人信息,新建仓库

git的每次操作都会显示操作者的姓名和邮箱,我也不知道如果不设置的话会显示什么内容,这里设置的方法如下:

  1. git config --global user.name 'Chenng'
  2. git config --global user.email 'ringcrl@foxmail.com'

新建仓库

  1. # 新建一个名字为 pot 的文件夹,里面有一个.git目录,pot 文件夹就是 git 仓库
  2. git init pot

基本操作

将文件加入跟踪并提交,提交三部曲:新增文件或修改文件、加入缓存区、提交到仓库

  1. # 新建一个文件
  2. echo line1 > file1
  3. # 查看 file1 文件的状态
  4. git status -s
  5. ?? file1
  6. # 将 file1 文件加入跟踪
  7. git add file1
  8. # 再查看状态
  9. git status -s
  10. A file1
  11. # 提交
  12. git commit -m 'init'
  13. bash

查看仓库修订

使用 git log 查看修订信息

  1. # head 指向最近一次修订,-N 用来指定输出总量
  2. git log head -1
  3. # 结果:q
  4. commit 7a2abdcce65c303bb41caa7fedc96f3a239e75e4
  5. Author: Chenng <ringcrl@foxmail.com>
  6. Date: Sat May 13 11:45:32 2017 +0800
  7. r2
  8. bash

使用 git show 显示一个指定修订的详细情况,如果不适用 --quiet 参数,就会显示此修订和它前一个修订之前的差异。

  1. # head~1 指示最近修订的前一个修订
  2. git show head~1 --quiet

使用参数 --pretty=oneline 查看更简洁的输出

  1. git log --pretty=one
  2. 7a2abdcce65c303bb41caa7fedc96f3a239e75e4 r2
  3. 39424918ca7b2a307df3e81391ebf83784d851c1 r1
  4. e25f263b47f51eb2ab73fc061443d83d7add271d init
  5. bash

使用缩写语法 --oneline 缩写 Commit 的标识符值

  1. git log --oneline
  2. # 结果
  3. 7a2abdc r2
  4. 3942491 r1
  5. e25f263 init
  6. bash

通过缩写的 Commit 的标识符值来查看修订信息

  1. git show 7a2abdc

父修订是指每个修订(除了第一个)都有父修订,一个修订可以有多个父修订,这种情况出现于分支合并的场景。

查看仓库修订的差异

使用 git diff 查看某一个文件的两次修订的差异

  1. git diff head~1 head file1
  2. # 结果
  3. diff --git a/file1 b/file1
  4. index c0d0fb4..83db48f 100644
  5. --- a/file1
  6. +++ b/file1
  7. @@ -1,2 +1,3 @@
  8. line1
  9. line2
  10. +line3
  11. bash

配置命令别名

使用 alias 来配置命令别名

  1. # 列出每个修订的提交消息
  2. git config --global alias.mist 'log --pretty=format:"%s"'

使用别名

  1. git mist -2
  2. # 结果
  3. r2
  4. r1
  5. bash

暂存区的理解

暂存区就像购物车,把需要的放进购物车,改变主意了可以随时拿掉,最后一次提交

  1. # 添加 bugfile1
  2. git add bugfile1
  3. # 添加 featurefile1
  4. git add featurefile1
  5. # 功能修改不应该放进来,把它拿出来
  6. git reset featurefile1
  7. # 添加 bugfile2
  8. git add bugfile2
  9. git commit -m 'bug fix for client'
  10. bash

撤销工作区文件修改

撤销某个文件的修改

  1. # 创建仓库、创建并提交文件到仓库
  2. git init p1 && cd p1
  3. echo line1 > file1
  4. echo line1 > file2
  5. echo line1 > file3
  6. git add file1 file2 file3
  7. git commit -m 'init'
  8. echo line2 >> file1
  9. echo line2 >> file2
  10. echo line2 >> file3
  11. git status -s
  12. # 结果
  13. M file1
  14. M file2
  15. M file3
  16. # 丢弃对 file1 的修改,checkout 有多重功能,这里的 -- 为了让 Git 知道后面的参数是文件名
  17. git checkout -- file1
  18. git status -s
  19. 结果
  20. M file2
  21. M file3
  22. # 可以使用 . 撤销全部的修改
  23. git checkout -- .
  24. bash

将文件移出暂存区

使用 git reset 将文件移出暂存区

  1. # 创建仓库、创建并提交文件到仓库
  2. git init p2 && cd p2
  3. echo line1 > file1
  4. echo line1 > file2
  5. echo line1 > file3
  6. git add .
  7. git status -s
  8. # 结果
  9. A file1
  10. A file2
  11. A file3
  12. # 将 file1 移出暂存区
  13. git reset file1
  14. git status -s
  15. # 结果
  16. A file2
  17. A file3
  18. ?? file1
  19. # 如果不指定文件参数,git reset 会将整个暂存区清空
  20. git reset
  21. git status -s
  22. # 结果
  23. ?? file1
  24. ?? file2
  25. ?? file3
  26. bash

撤销提交

即使修改已经提交到仓库,也是可以撤销的。

  1. git init p4 && cd p4
  2. echo line1 > file1
  3. git add .
  4. git commit -m 'r1'
  5. echo line2 >> file1
  6. git commit -m 'r2' -a
  7. echo line3 >> file1
  8. git commit -m 'r3' -a
  9. git mist
  10. # 结果
  11. r3
  12. r2
  13. r1
  14. # 使用 git reset 撤销到指定修订处
  15. git reset head~1
  16. git mist
  17. # 结果
  18. r2
  19. r1
  20. git diff
  21. # 结果
  22. diff --git a/file1 b/file1
  23. index c0d0fb4..83db48f 100644
  24. --- a/file1
  25. +++ b/file1
  26. @@ -1,2 +1,3 @@
  27. line1
  28. line2
  29. +line3
  30. # 因为撤销提交,新添加的 line3 回到了 暂存区状态
  31. bash

恢复撤销的提交

可以把 git reset head~1 所导致的撤销恢复

  1. # 列出全部操作
  2. git reflog
  3. # 结果
  4. bb9de36 HEAD@{0}: reset: moving to head~1
  5. ee4882a HEAD@{1}: commit: r3
  6. bb9de36 HEAD@{2}: commit: r2
  7. e6c363a HEAD@{3}: commit (initial): r1
  8. # 回到 HEAD@{1}
  9. git reset head@{1}
  10. git mist
  11. # 结果
  12. r3
  13. r2
  14. r1
  15. bash

撤销提交内的部分文件

原理是撤销整次提交,然后将 add 后文件里面不需要的移出,再次提交

  1. git init p4 && cd p4
  2. echo line1 > file1
  3. echo line1 > file2
  4. git add .
  5. git commit -m 'init'
  6. echo line2 >> file1
  7. echo line2 >> file2
  8. git commit -m 'r2' -a
  9. # 撤销最后一次提交
  10. git reset head~
  11. git add .
  12. # 在提交中移出对 file1 的更改
  13. git reset file1
  14. git commit -m 'r2'
  15. git status -s
  16. # 结果
  17. M file1
  18. bash

创建分支

创建分支还是走之前那个撤销修改的 checkout

  1. git init p1 && cd p1
  2. echo line1 > file1
  3. echo line2 >> file1
  4. echo line3 >> file1
  5. git add .
  6. git commit -m 'init'
  7. # 查看分支列表,*代表当前分支
  8. git branch
  9. # 结果
  10. * master
  11. # 创建并切换到新分支
  12. git checkout -b roma
  13. # 再次查看分支
  14. git branch
  15. # 结果
  16. *roma
  17. master
  18. bash

在新分支修改代码

在 roma 分支上多次修改代码达到目标

  1. sed -i '' 's/line1/lineI/g' file1
  2. git add file1
  3. git commit -m 'roma 1'
  4. sed -i '' 's/line2/lineII/g' file1
  5. git add file1
  6. git commit -m 'roma 2'
  7. sed -i '' 's/line3/lineIII/g' file1
  8. git add file1
  9. git commit -m 'roma 3'
  10. git mist
  11. # 结果
  12. roma 3
  13. roma 2
  14. roma 1
  15. init
  16. bash

切换分支

现在需要将 roma 分支的修改合回 master

  1. # 先切回 master 分支
  2. git checkout master
  3. cat file1
  4. # 结果
  5. line1
  6. line2
  7. line3
  8. git merge roma
  9. cat file1
  10. # 结果
  11. lineI
  12. lineII
  13. lineIII
  14. # 删除 roma 分支
  15. git branch -d roma
  16. bash

解决冲突

多个分支改了同一个地方会引发冲突

  1. git init p2 && cd p2
  2. # 修改、暂存、提交
  3. echo line1 > file1
  4. echo line2 >> file1
  5. git add file1
  6. git commit -m 'init'
  7. # 创建并切换到 roma 分支
  8. git checkout -b roma
  9. sed -i '' 's/line2/lineII/g' file1
  10. git add file1
  11. git commit -m 'roma 1'
  12. # 回到 master 分支,并修改同一处地方
  13. git checkout master
  14. sed -i '' 's/line2/lineTwo/g' file1
  15. git add file1
  16. git commit -m 'commit 2'
  17. # 把 roma 分支合并到 master 上
  18. git merge roma
  19. # 报错了,但是文件已经被修改
  20. Auto-merging file1
  21. CONFLICT (content): Merge conflict in file1
  22. Automatic merge failed; fix conflicts and then commit the result.
  23. # 手动修改文件,将冲突部分删除,然后 git commit
  24. vi file1 ...
  25. git add file1
  26. git commit -m 'conflict solved'
  27. # 删除多余分支
  28. git branch -d roma
  29. bash

.gitignore 配置

  • 匹配模式前 / 代表项目根目录
  • 匹配模式后 / 代表是目录
  • 匹配模式前加 ! 代表取反
  • * 代表任意个字符
  • ? 匹配任意一个字符
  • ** 匹配多级目录