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
最新文章
- Atitit 项目管理(5)----------后勤管理与工具链支持管理
- 单元测试与Nunit的基本使用
- WebSocket 服务器4
- [转]Android WebView播放视频(包括全屏播放),androidwebview
- grunt安装失败处理
- PHP Mysql类【转】
- js判断年龄是否在0-100之间
- Summary Ranges leetcode
- Lists, Maps and Sets in Java
- [array] leetcode - 54. Spiral Matrix - Medium
- PHP秒杀系统 高并发高性能的极致挑战(完整版)
- STM32L476RG_中断开发与实列
- MyBatis的好处及常见问题
- Liunx之KVM搭建图形化的WEB
- Linux_安装
- javascript替代Array.prototype.some操作
- 在ALV界面添加下拉框
- 1.数据结构&;算法的引言+时间复杂度
- SQL记录-PLSQL触发器
- jquery.fly.min.js 拋物插件
热门文章
- 通过shell脚本开始和结束守护进程
- 查询sql语句的执行时间
- signalR之java client的websocket BUG处理
- SQLyog使用,连接ubuntu虚拟机(Error No.2003)
- javascript window.showModalDialog不兼容goole解决方案
- Binder学习笔记(十二)—— binder_transaction(...)都干了什么?
- <;c:choose>;标签内出错。不能写注解,否则就会报错
- Mole and Abandoned Mine
- Java——事务
- 2017第八届蓝桥杯决赛(C++ B组)2.磁砖样式