Q:

I wrote the wrong thing in a commit message. Alternatively, I've forgotten to include some files.

How can I change the commit message/files? The commit has not been pushed yet.

A:

有两种方法:

1.

Amending the most recent commit message

git commit --amend

will open your editor, allowing you to change the commit message of the most recent commit. Additionally, you can set the commit message directly in the command line with:

git commit --amend -m "New commit message"

…however, this can make multi-line commit messages or small corrections more cumbersome to enter.

Make sure you don't have any working copy changes staged before doing this or they will get committed too. (Unstaged changes will not get committed.)

Changing the message of a commit that you've already pushed to your remote branch

If you've already pushed your commit up to your remote branch, then you'll need to force push the commit with:

git push <remote> <branch> --force
# Or
git push <remote> <branch> -f

Warning: force-pushing will overwrite the remote branch with the state of your local one. If there are commits on the remote branch that you don't have in your local branch, you willlose those commits.

Warning: be cautious about amending commits that you have already shared with other people. Amending commits essentially rewrites them to have different SHA IDs, which poses a problem if other people have copies of the old commit that you've rewritten. Anyone who has a copy of the old commit will need to synchronize their work with your newly re-written commit, which can sometimes be difficult, so make sure you coordinate with others when attempting to rewrite shared commit history, or just avoid rewriting shared commits altogether.

2.

Use interactive rebase

Another option is to use interactive rebase.
This allows you to edit any message you want to update even if it's not the latest message.

In order to do a git squash, follow these steps:

// X is the number of commits to the last commit you want to be able to edit
git rebase -i HEAD~X

Once you squash your commits - choose the e/r for editing the message

Important note about Interactive rebase

When you use the git rebase -i HEAD~X there can be more than X commits. Git will "collect" all the commits in the last X commits and if there was a merge somewhere in between that range you will see all the commits as well so the outcome will be X+.

Good tip:

If you have to do it for more than a single branch and you might face conflicts when amending the content, set up git rerere and let git resolve those conflicts automatically for you.

git rebase 的使用 (用于撤销某次commit)的更多相关文章

  1. git rebase简介(基本篇)

    原文: http://gitbook.liuhui998.com/4_2.html 一.基本 git rebase用于把一个分支的修改合并到当前分支. 假设你现在基于远程分支"origin& ...

  2. Git git rebase 使用

      原文:http://gitbook.liuhui998.com/4_2.html 一.基本 git rebase用于把一个分支的修改合并到当前分支. 假设你现在基于远程分支"origin ...

  3. git rebase 使用

    git rebase 不会取回代码 要用git fetch先取回, git rebase 是合并代码. (1)首先用git fetch返回服务器上的代码 (2)首先用git rebase origin ...

  4. git<git rebase 修改以前提交过的内容>

      git rebase 使用总结: 使用git rebase 修改以前已经提交的内容 比如要修改之前的commit的 hashcode为:187f869c9d54c9297d6b0b1b4ff47d ...

  5. 关于git rebase的相关讲解

    http://gitbook.liuhui998.com/4_2.html 一.基本 git rebase用于把一个分支的修改合并到当前分支. 假设你现在基于远程分支"origin" ...

  6. git rebase 和 git merge 总结

    git merge 和 git rebase 都是用于合并分支,但二者是存在区别的. 在使用时,记住以下两点: 当你从 remote 去 pull 的时候,永远使用 rebase(除了一个例外) 当你 ...

  7. 【转】git rebase简介(基本篇)

    原文网址:http://blog.csdn.net/hudashi/article/details/7664631/ 原文: http://gitbook.liuhui998.com/4_2.html ...

  8. git 必看,各种撤销操作

    场景概念说明 首先说明一个概念, git是一个分布式的版本控制工具,分布式即 git 管理的项目是有多个大致平等的仓库的.通过一个例子来说明这个东西. 举一个最简单的使用场景: 你在github 建立 ...

  9. (转)git rebase 命令

    转自:http://blog.csdn.net/hudashi/article/details/7664631/ 原文: http://gitbook.liuhui998.com/4_2.html 一 ...

随机推荐

  1. Linux就该这么学 20181004(第六章磁盘管理)

    参考链接https://www.linuxprobe.com/ /boot 开机锁需要文件-内核.开机菜单以及所需配置文件 /dev 以文件形式存放的任何设备与接口 /etc 配置文件 /home 用 ...

  2. spm 捕获

    目录 自动捕获 从library cache 中加载 从SQL 调优级加载 1.自动捕获 当optimizer_capture_sql_plan_baselines参数设置为TRUE,查询优化器自动存 ...

  3. Windows 10 10586 升级

  4. CSS3的常用属性(二)

    边框 边框圆角 border-radius: 100px 每个角可以设置两个值,x和y 补充: 可分别设置长,短半径,以“/”进行分隔,遵循顺时针的顺序,“/”之前为横轴半径,“/”之后为纵轴半径,如 ...

  5. Android setImageResource与setImageBitmap的区别

    同样的布局文件,小分辨率手机: 1.使用setImageBitmap设置时,出现如下现象: 2.使用setImageResource时,图片显示正常 原因:setImageResource(id)会根 ...

  6. swift 20 - Nested Types

    Nested Types 只是为了方便类型的整合和使用 struct BlackjackCard { // nested Suit enumeration enum Suit: Character { ...

  7. json转换方法

    import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; import java.ut ...

  8. DedeCMS搜索结果页面调用自定义字段的方法

    有时候在我们需要在dedecms的搜索结果页面调用自定义字段,尤其是在做下载站的时候,需要在搜索结果页调用软件大小以及软件等级等等,但是我们发现在搜索结果页模板中使用“[field:字段名]”标签无法 ...

  9. React显示文件夹中SVG

    import React from 'react'; import _ from 'lodash'; import styles from './iconPicker.less'; const req ...

  10. npm install报错类似于npm WARN tar ENOENT: no such file or directory, open '***\node_modules\.staging\***

    报错类似于如下图 解决方法: 删除文件 package-lock.json,再重新执行npm i或者npm install