git使用笔记-提高篇

一、分支、合并

  1、合并一个特定提交 a specific commit

    git cherry-pick commit-id 把commit-id代表的本次提交合并到当前分支,如果有冲突需要解决后,提交

   2、fast-forward 和 non-fast-forward

    比如有master分支,最新提交commit-id-1,现在基于master分支新建develop分支,在develop分支开发提交commit-id-2,那么现在把develop合并(git merge)到master分支,则只需要移动master的ref到commit-id-2,不会生成一个新的commit-id,然后删除develop分支,那么现在的git log --graph里面根本不会看到develop分支。如果使用git merge --no-ff develop,则会生成一个新的提交commit-id,在删除develop分支后,git log --graph可以看到develop分支以及分支合并情况。这两种方式各有利弊:1)方式1使得log简洁,2)方式2可以重现合并记录,体现开发过程。

The --no-ff flag prevents git merge from executing a "fast-forward" if it detects that your current HEAD is an ancestor of the commit you're trying to merge. A fast-forward is when, instead of constructing a merge commit, git just moves your branch pointer to point at the incoming commit. This commonly occurs when doing a git pull without any local changes.

However, occasionally you want to prevent this behavior from happening, typically because you want to maintain a specific branch topology (e.g. you're merging in a topic branch and you want to ensure it looks that way when reading history). In order to do that, you can pass the --no-ff flag and git merge will always construct a merge instead of fast-forwarding.

Similarly, if you want to execute a git pull or use git merge in order to explicitly fast-forward, and you want to bail out if it can't fast-forward, then you can use the --ff-only flag. This way you can regularly do something like git pull --ff-only without thinking, and then if it errors out you can go back and decide if you want to merge or rebase.

        3、git commit --amend回退

    git commit --amend会合并本次和最后一次提交commit-last,生成新的一个新的commit-id,commit-last会被删除。这时要回退--amend提交,则需要找回commit-last。git reflog工具可以记录HEAD的每一次变化,那么可以查到commit-last,使用git reset commit-last即可回退。

   4、删除某次提交specific commit

  这种情况需要先用git log命令在历史记录中查找到想要删除的某次提交的commit id,比如下图中圈出来的就是注释为”2”的提交的commit id(由此可见提交的注释很重要,一定要认真写)

        这里写图片描述

        然后执行以下命令(”commit id”替换为想要删除的提交的”commit id”,需要注意最后的^号,意思是commit id的前一次提交):

    git rebase -i "commit id"^

         执行该条命令之后会打开一个编辑框,内容如下,列出了包含该次提交在内之后的所有提交。

        这里写图片描述

       然后在编辑框中删除你想要删除的提交所在行,然后保存退出就好啦,如果有冲突的需要解决冲突。接下来,执行以下命令,将本地仓库提交到远程库就完成了:

    git push origin master -f

       5、修改历史某次提交

        这种情况的解决方法类似于第二种情况,只需要在第二条打开编辑框之后,将你想要修改的提交所在行的pick替换成edit然后保存退出,这个时候rebase会停在你要修改的提交,然后做你需要的修改,修改完毕之后,执行以下命令:

    git add .
    git commit --amend
    git rebase --continue

        如果你在之前的编辑框修改了n行,也就是说要对n次提交做修改,则需要重复执行以上步骤n次。

         需要注意的是,在执行rebase命令对指定提交修改或删除之后,该次提交之后的所有提交的”commit id”都会改变。

二 排除不需要的文件

  不需要上传的文件放在gitignore里面过滤。

  可以用git check-ignore命令检查:

  $ git check-ignore -v App.class
  .gitignore:3:*.class    App.class

参考:

  https://backlog.com/git-tutorial/cn/stepup/stepup7_4.html

  https://blog.csdn.net/QQxiaoqiang1573/article/details/68074847

  https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013758404317281e54b6f5375640abbb11e67be4cd49e0000

原文地址:https://www.cnblogs.com/shihuvini/p/9316991.html