Git - Peace&Love

文章目录
  1. 1. Git 常用命令
    1. 1.1. add
    2. 1.2. commit
    3. 1.3. checkout
    4. 1.4. reset
    5. 1.5. diff
    6. 1.6. status
    7. 1.7. merge
    8. 1.8. log
    9. 1.9. stash
    10. 1.10. rebase
      1. 1.10.1. rebase
      2. 1.10.2. abort
      3. 1.10.3. 压缩提交
      4. 1.10.4. 粘贴提交
      5. 1.10.5. —onto
    11. 1.11. revert
    12. 1.12. blame
    13. 1.13. remote
    14. 1.14. push
    15. 1.15. pull
    16. 1.16. bisect
    17. 1.17. Git Pointer
  2. 2. Git 合作注意事项
    1. 2.1. 3 states in Git
    2. 2.2. merge rebase的讨论

Git 使用便捷上手册

git push origin newbranch

  • 推送本地newbranch 到origin 上

git 有三个区域:工作区、暂存区、版本库(working/staging/repository)。

Git 常用命令

add

从工作区(working directory/tree)添加文件到暂存区(staging area,index)

commit

checkout

创建一个本地分支dev并切换到该分支, git checkout -b dev

reset

1
- A - B - C (master)
  • reset --soft B, master (and thus HEAD) now points to B, but the index still has the changes from C; git status will show them as staged. So if we run git commit at this point, we’ll get a new commit with the same changes as C.

  • git reset --mixed B``master and HEAD point to B, but this time the index is also modified to match B. [git commit 会有什么输出结果?]

    If we run git commit at this point, nothing will happen since the index matches HEAD. We still have the changes in the working directory, but since they’re not in the index, git status shows them as unstaged.

  • --hard, 与—mixed类似,除此之外它还恢复工作区内容

    git reset --hard B the changes added in C, as well as any uncommitted changes you have, will be removed, and the files in your working copy will match commit B.(在C处已add的文件修改也会被discard)撤销工作区和暂存区的任何改变,动作不可逆,危险

diff

git diff origi new, outputs new 相对于origi的变动

git diff : 比较 暂存区 相对于 工作区 的改动

git diff —stage : 比较 仓库 相对于 暂存区的改动

git diff commit1 commit2 : 比较commit2相对于commit1的改动

git show commit1, 比较commit1相对于其父母提交(commit)的改动

status

查看三个区的状态,无参数时比较的暂存区与工作区的异同

merge

git merge

git merge branch, 将branch分支合并到当前分支

fast-merge,三方合并as a commit

log

git log [—graph] [—oneline]

  • 当前commit及之前的commit log提交日志
  • 如果要查看(当前节点之后)的提交日志,可以使用 git reflog

stash

git stash - Stash the changes in a dirty working directory away

git stash (== git stash push)

git stash pop : 从存储中弹出最上面的stash,并应用在当前工作区上,再删除此stash

git stash list : 列出当前存储的 stash

git stash save:同 git stash,但是可以添加一些注释
git stash apply : 将堆栈中的内容应用到当前目录而不删除
git stash drop Name : 从堆栈中移除某个指定stash
git stash show : 查看堆栈中最新的stash与当前目录差异
git stash branch : 从最新的stash创建分支

rebase

什么是base?base, (basic/basement),提交commit的base,就像树根,是一个分支的基础。一个分支的基础又该怎么理解呢?就是一个分支从哪里衍生而来。所以它的基础就是它之前的提交。

到这里,变基,改变基础,的意思就很明显了。就是改变之前的提交。

那怎么改变之前的提交呢?有多种改变方式,一种简单的情况:请设想分支1和分支2从一个共同的提交b像树杈一样分离,此时提交b为分支1 和 分支2共同的基。想改变分支2的基,使分支1也为分支2的基。

git rebase(变基,改变commit的根基)内容上看起来就像是把分支的基础从一个commit更改为另一个commit

The primary reason for rebasing is to maintain a linear project history. 维护线性历史

变基是集成上游改变到本地仓库的一般方式。如果用merge的话,每次想看远程仓库的改变都会有多余的merge commit。变基的意思就像是,我想基于其他人的改变来做我的改变。


rebase

link

1
git rebase [<upstream>] [<branch>]

img

git rebase master other_branch, 将other_branch分支的基切换为master指向的commit

img


git rebase —onto server client, 将client区别于server分支的提交在master分支上面重放一次,完成之后client分支移动到master分支 master指针之后。

git rebase branch,把当前分支区别于master分支的提交在branch分支上重放一次,当前分支指针也就变动到了branch分支上

git pull —rebase,将远程分支来下来,并且使远程分支为本地的改变之基

abort

git rebase —abort

压缩提交

  • 合并提交/压缩提交

    git rebase startCommit eCommit: 以sCommit为基进行变基

    将eCommit的基调整为startCommit:压缩(startCommit, endCommit]之间的提交为一个,需要手动选择对(startCommit, endCommit]间的每一个提交的操作方式。

粘贴提交

  • 将一段commits粘贴到另一个分支上

    git rebase sCommit endCommit —onto branchName

    将 (start commit, end commit] 之间的commits拷贝到 分支bName后面,并且把bName分支移动到最后?

    img

    欲完成上述功能,执行命令:git rebase b^ e --onto master

如果提交存在于你的仓库之外,而别人可能基于这些提交进行开发,那么不要执行变基。

As we’ve discussed previously in rewriting history, you should never rebase commits once they’ve been pushed to a public repository. The rebase would replace the old commits with new ones and it would look like that part of your project history abruptly vanished.


—onto

img

git rebase [—onto ] [ []]

git rebase —onto master branch_one branch_two,将b2区别于b1的提交在master上面重放一次,并且将b2移动到最新的提交上面

img

revert

git revert aCommit : 在产生的新提交(commit)中,撤销自某个aCommit引入的所有改变。

之后ref指针更新到产生的新提交。保持提交历史不变。


需要解决冲突,git会将aCommit前后分支的内容体现在产生的冲突的文档中。

常用选项options:

  • -e —edit: 默认,打开编辑器以编辑提交信息
  • —no-edit: 上面的反面
  • -n —no-commit: 这个选项会将自aCommit以来所引入的所有修改都放在工作区和暂存区中。

  • 这个东西和我有什么关联?

    通过学习这个命令,会提高我利用git进行代码历史管理的能力

  • 命令可能运用场景是什么?

    当我把历史已经提交到远程,其他人可能已经基于历史做了改变。为了不修改历史,可以使用revert命令将代码回退到特定提交。

  • 有什么辅助东西可以帮我更好地运用这个东西吗?

    撤销命令-已经提交到公开仓库-不修改历史

blame

git blame : 列出文件每一行上次修改commit及作者

可以在历史中追踪某段代码什么时候从文件中添加、删除、移动、替换。

git log -S'blame-usage'

remote

git remote -v

git remote remove origin : rm 远程origin

git remote add origin master git:SSH-link

push

推送本地分支到远程仓库(远程仓库无此分支) git push —set-upstream origin dev

从远程拉取一个本地不存在的分支:git checkout -b 本地分支名 origin/远程分支名

git checkout afile,可以检出仓库中的一个文件,删除掉在工作区所做改变

pull

git pull origin master <=> git fetch origin , then git merge master origin/master

bisect

Git Pointer

git pointer GIT中的指针 HEAD/ORIGIN

HEAD, 指向当前分支的上一次提交 last commit

Git 合作注意事项

  1. 每天第一次修改文件时,确保已pull远程的文件并且merge。
  2. 重要的提交(commit),注释的信息详细一点。
    1. 第一行是提交的整体描述信息
    2. 再空一行,第三行开始记录更为细致的提交信息

https://www.cnblogs.com/tsingke/p/10853936.html)

3 states in Git

https://stackoverflow.com/questions/3689838/whats-the-difference-between-head-working-tree-and-index-in-git

img

Notes:

  1. the workspace is the directory tree of (source) files that you see and edit.
  1. The index is a single, large, binary file in <baseOfRepo>/.git/index, which lists all files in the current branch, their sha1 checksums, time stamps and the file name — it is not another directory with a copy of files in it. 待提交的修改会被放在index中

    1. The local repository is a hidden directory (.git) including an objects directory containing all versions of every file in the repo (local branches and copies of remote branches) as a compressed “blob” file.

    Don’t think of the four ‘disks’ represented in the image above as separate copies of the repo files.

img

In the above image “working directory” is the same as “working tree”, the “staging area” is an alternate name for git “index”, and HEAD points to currently checked out branch, which tip points to last commit in the “git directory (repository)”

Note that git commit -a would stage changes and commit in one step.

HEAD is a pointer to the branch or commit that you last checked out, and which will be the parent of a new commit if you make it. For instance, if you’re on the master branch, then HEAD will point to master, and when you commit, that new commit will be a descendent of the revision that master pointed to, and master will be updated to point to the new commit.

merge rebase的讨论

merge用于合并两个分支;rebase用于将一个分支的基改为另一个,如下图,命令git rebase master other-branch,将把other-branch的基从C变为E。

img

因此rebase将更改历史,提交的公开仓库的分支尽量不要使用。

而merge是增量改变,不会触碰提交历史,可以在公开分支上面使用。