[Practical Git] Clean up commits with git rebase

Sometimes its nice to clean up commits before merging them into your main code repo; in this lesson, we go over using git rebase to squash commits together and then rename the condensed commit message. We also talk about potential issues with rebasing and where to be careful.

//First, you can fetch the remote branch
git fetch

//Then can see the logs between remote branch and local branch
git log origin/master..
git rebase -i origin/master

One thing to note is that a rebase is destructive. It actually changes your Git history. You shouldn‘t use a rebase on code that‘s already been put in your master branch on your remote repository that other developers might be using. A rebase has the same function as a Git merge, but it cleans up and destroys history, whereas a merge preserves all history, and includes a merge commit.

The bottom line is that, as long as you only need to clean up commits that you‘ve made locally or in a pull request branch, you can use rebase to clean them up before you merge them into your main master branch.

If you have already pushed your commits to a pull request branch, then after you run the rebase, because it‘s destructive, you‘ll need to run:

git push -f

, for force, to let Git know that you‘re OK with destroying the history that‘s in a remote branch.

Again, be careful with this, and only use a rebase and a force push if you‘re working on code that hasn‘t been made public yet. One other thing to note is that, if at any time during a rebase, you realize you‘ve made a mistake, you can get run the Git rebase command with the abort flag to stop the rebase, and return your repo to its state before you started the rebase.

git rebase --abort
时间: 2024-08-09 10:31:42

[Practical Git] Clean up commits with git rebase的相关文章

git clean使用总结

git clean移除工作区中untracked的文件(即:需要先add).一直都是用rm来操作--?? 1.git clean -n:列出会被删除的文件,未删除 2.git clean -f:force, 执行删除,不会删除.gitignore中指定的文件 clean.requireForce设置为false可不用带-f,直接使用git clean. 3.git clean -f <path>:删除指定路径的文件 4.git clean -df:删除文件和目录 5.git clean -xf

Git合并特定commits 到另一个分支

https://ariejan.net/2010/06/10/cherry-picking-specific-commits-from-another-branch/ http://blog.csdn.net/ybdesire/article/details/42145597 经常被问到如何从一个分支合并特定的commits到另一个分支.有时候你需要这样做,只合并你需要的那些commits,不需要的commits就不合并进去了. 合并某个分支上的单个commit 首先,用git log或GitX

Git Step by Step – (8) Git的merge和rebase

前面一篇文章中提到了"git pull"等价于"git fetch"加上"git merge",然后还提到了pull命令支持rebase模式,这篇文章就介绍一下merge和rebase之间有什么差别. 由于我们主要是想看看merge跟rebase之间的区别,这里就是用本地仓库的分支进行演示了. merge 其实在介绍分支的那篇文章中已经介绍过了一些分支merge的内容,这里就进行一些补充和总结. 下面我们基于本地一个仓库开始介绍,当前仓库的分支情

你可能不知道的git clean

git clean是干啥的? git clean可以帮你清理workspace中未被git版本控制的文件,比如临时文件,构建出来的二进制文件. 使用方法 如果你的clean.requireForce项没有设置为false,那么每次使用git clean时都要加上-f参数 示例如下,workspace中有2个调试logo时创建的临时png文件,commit之前需要删掉.如果不用git clean的话需要一个一个rm. [email protected]:42:26:~/Angular/pomodo

git clean

git clean命令用来从你的工作目录中删除所有没有tracked过的文件. git clean经常和git reset --hard一起结合使用. 记住reset只影响被track过的文件, 所以需要clean来删除没有track过的文件. 结合使用这两个命令能让你的工作目录完全回到一个指定的<commit>的状态. 用法 git clean -n 是一次clean的演习, 告诉你哪些文件会被删除. 记住他不会真正的删除文件, 只是一个提醒. git clean -f 删除当前目录下所有没

git代码合并:Merge、Rebase的选择

代码合并:Merge.Rebase的选择 Zhongyi Tong edited this page on Dec 7, 2015 · 3 revisions Pages 19 Home 2.1 快速指南 2.2 创建代码仓库 2.3 保存你的更改 2.4 检查仓库状态 2.5 检出之前的提交 2.6 回滚错误的修改 2.7 重写项目历史 3.2 保持同步 3.3 创建Pull Request 3.4 使用分支 3.5 常见工作流比较 4.1 图解Git命令 5.1 代码合并:Merge.Reb

[转载][GitHub]How to undo the most recent commits in Git?

原文地址:https://stackoverflow.com/questions/927358/how-to-undo-the-most-recent-commits-in-git/927386#927386 git reset最详尽介绍:https://git-scm.com/docs/git-reset 总结一下: git reset --hard HEAD~1git push --force 值得注意的是,这类操作比较比较危险,例如:在你的commit之后别人又提交了新的commit,那在

[Practical Git] Format commit history with git log arguments

When running the git log command, we can pass in options as arguments toformat the data shown for each commit. In this lesson, we show how to use the oneline, decorate, graph, stat, and p options with git log. Show it oneline: git log --oneline git l

Git专题--系统的学习Git之一

本文为整理自:伯乐在线 1.Git详解之一:Git起步 2.Git详解之二:Git基础 3.Git详解之三:Git分支 4.Git详解之四:服务器上的Git 5.Git详解之五:分布式Git 6.Git详解之六:Git工具 7.Git详解之七:自定义Git 8.Git详解之八:Git与其他系统 9.Git详解之八:Git与其他系统 Git详解之一:Git起步 起步 本章介绍开始使用 Git 前的相关知识.我们会先了解一些版本控制工具的历史背景,然后试着让 Git 在你的系统上跑起来,直到最后配置