Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/Documentation/git-history.adoc # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / Documentation / git-history.adoc` 312 lines ยท 11603 bytes Raw Blame History 1 git-history(1) 2 ============== 3 4 NAME 5 ---- 6 git-history - EXPERIMENTAL: Rewrite history 7 8 SYNOPSIS 9 -------- 10 [synopsis] 11 git history drop [--dry-run] [--update-refs=(branches|head)] [--empty=(drop|keep|abort)] [--[no-]gpg-sign[=]] 12 git history fixup [--dry-run] [--update-refs=(branches|head)] [--reedit-message] [--empty=(drop|keep|abort)] [--[no-]gpg-sign[=]] 13 git history reword [--dry-run] [--update-refs=(branches|head)] [--[no-]gpg-sign[=]] 14 git history split [--dry-run] [--update-refs=(branches|head)] [--[no-]gpg-sign[=]] [--] [...] 15 git history squash [--dry-run] [--update-refs=(branches|head)] [--[no-]edit] [--[no-]gpg-sign[=]] 16 17 DESCRIPTION 18 ----------- 19 20 Rewrite history by rearranging or modifying specific commits in the 21 history. 22 23 THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE. 24 25 This command is related to linkgit:git-rebase[1] in that both commands can be 26 used to rewrite history. There are a couple of major differences though: 27 28 * Most subcommands of linkgit:git-history[1] can work in a bare repository as 29 they do not need to touch either the index or the worktree. The `fixup` 30 subcommand is an exception to this, as it reads staged changes from the index. 31 * linkgit:git-history[1] does not execute any linkgit:githooks[5] at the 32 current point in time. This may change in the future. 33 * linkgit:git-history[1] by default updates all branches that are descendants 34 of the original commit to point to the rewritten commit. 35 36 Overall, linkgit:git-history[1] aims to provide a more opinionated way to modify 37 your commit history that is simpler to use compared to linkgit:git-rebase[1] in 38 general. 39 40 Use linkgit:git-rebase[1] if you want to reapply a range of commits onto a 41 different base, or interactive rebases if you want to edit a range of commits 42 at once. 43 44 LIMITATIONS 45 ----------- 46 47 This command does not (yet) replay merge commits onto the rewritten 48 history: if a commit that would be replayed is a merge, the operation is 49 rejected, and you should use linkgit:git-rebase[1] with the 50 `--rebase-merges` flag instead. The `squash` subcommand can still fold merges 51 that lie inside the selected range, subject to the restrictions below. 52 53 Furthermore, the command does not support operations that can result in merge 54 conflicts. This limitation is by design as history rewrites are not intended to 55 be stateful operations. The limitation can be lifted once (if) Git learns about 56 first-class conflicts. 57 58 When using `fixup` with `--empty=drop`, dropping the root commit is not yet 59 supported. Likewise, `drop` cannot remove the root commit or a merge commit. 60 61 COMMANDS 62 -------- 63 64 The following commands are available to rewrite history in different ways: 65 66 `drop `:: 67 Remove the specified commit from the history. All descendants of the 68 commit are replayed directly onto its parent. 69 + 70 The root commit cannot be dropped as that may lead to edge cases where refs 71 end up with no commits anymore. Merge commits cannot be dropped either; see 72 LIMITATIONS. 73 + 74 If `HEAD` points at a commit that is to be rewritten, the index and working 75 tree are updated to match the new `HEAD`. The command aborts before any 76 references are updated in case local modifications would be overwritten. 77 + 78 If replaying any descendant would result in a conflict, the command aborts 79 with an error. 80 81 `fixup `:: 82 Apply the currently staged changes to the specified commit. This is 83 similar in nature to `git commit --fixup=` followed by `git 84 rebase --autosquash ~`. Changes are applied to the target 85 commit by performing a three-way merge between the HEAD commit, the 86 target commit and the tree generated from staged changes. 87 + 88 The commit message and authorship of the target commit are preserved by 89 default, unless you specify `--reedit-message`. 90 + 91 If applying the staged changes would result in a conflict, the command 92 aborts with an error. All branches that are descendants of the original 93 commit are updated to point to the rewritten history. 94 95 `reword `:: 96 Rewrite the commit message of the specified commit. All the other 97 details of this commit remain unchanged. This command will spawn an 98 editor with the current message of that commit. 99 100 `split [--] [...]`:: 101 Interactively split up into two commits by choosing 102 hunks introduced by it that will be moved into the new split-out 103 commit. These hunks will then be written into a new commit that 104 becomes the parent of the previous commit. The original commit 105 stays intact, except that its parent will be the newly split-out 106 commit. 107 + 108 The commit messages of the split-up commits will be asked for by launching 109 the configured editor. Authorship of the commit will be the same as for the 110 original commit. 111 + 112 If passed, __ can be used to limit which changes shall be split out 113 of the original commit. Files not matching any of the pathspecs will remain 114 part of the original commit. For more details, see the 'pathspec' entry in 115 linkgit:gitglossary[7]. 116 + 117 It is invalid to select either all or no hunks, as that would lead to 118 one of the commits becoming empty. 119 120 `squash `:: 121 Fold all commits in __ into the oldest commit of that 122 range. The resulting commit keeps the oldest commit's authorship and 123 takes the tree of the range's newest commit, so the whole range 124 collapses into a single commit. Commits above the range are replayed 125 on top of the result. 126 + 127 The range is given in the usual `..` form, where __ is 128 the commit just below the oldest commit to squash. For example, `git 129 history squash HEAD~3..HEAD` folds the three most recent commits into 130 one, and `git history squash HEAD~5..HEAD~2` squashes an interior range 131 while leaving the two newest commits in place. Several revisions may be 132 given, for example `HEAD~3..HEAD ^topic` to additionally exclude what is 133 already on `topic`. Rev-list options may also be given, but any that would 134 change how the range is walked are overridden with a warning. 135 + 136 An editor opens pre-filled with the messages of all the folded commits so you 137 can combine them. With `--no-edit`, the oldest commit's message is preserved 138 instead, except that an `amend!` commit targeting it replaces its message. 139 + 140 The selected commits must form a connected graph with a single tip and must 141 not include a root commit. Every parent of a commit after the oldest one must 142 either be selected or also be a parent of the oldest commit. When the oldest 143 commit is a merge, all of its parents are preserved in the squashed commit. 144 + 145 A `fixup!`, `squash!`, or `amend!` commit is refused unless the commit it 146 targets is also in the range, so the fold does not silently absorb a 147 marker meant for a commit outside it. As an exception, a range made up entirely 148 of markers for one target is combined into a single commit. With `--no-edit`, 149 the last `amend!` message is used if there is one; a `squash!` or `amend!` is 150 otherwise refused if folding it would discard its message. 151 + 152 The editor template mirrors `git rebase -i --autosquash`: each `fixup!`, 153 `squash!`, or `amend!` is grouped under the commit it targets rather than 154 shown in commit order. A `fixup!` message is dropped (commented out in full), 155 a `squash!` keeps its body with only the marker subject commented, and an 156 `amend!` replaces its target's message, unless a `squash!` folded into that 157 target first, in which case it keeps its body like a `squash!`. 158 + 159 A local branch descended from a selected commit but not from the range tip 160 cannot be rewritten as a descendant of the result, so with the default 161 `--update-refs=branches` the command refuses. Rerun with `--update-refs=head` 162 to rewrite only the current branch and leave such branches unchanged. Tags 163 and remote-tracking refs are always left unchanged. 164 165 OPTIONS 166 ------- 167 168 `--dry-run`:: 169 Do not update any references, but instead print any ref updates in a 170 format that can be consumed by linkgit:git-update-ref[1]. Necessary new 171 objects will be written into the repository, so applying these printed 172 ref updates is generally safe. 173 174 `--edit`:: 175 `--no-edit`:: 176 For `squash`, open an editor to combine the messages of the folded commits. 177 This is the default; use `--no-edit` to keep the selected message without 178 opening an editor. 179 180 `--reedit-message`:: 181 Open an editor to modify the target commit's message. 182 183 `-S[]`:: 184 `--gpg-sign[=]`:: 185 `--no-gpg-sign`:: 186 GPG-sign rewritten commits. The __ argument is optional and 187 defaults to the committer identity; if specified, it must be stuck to 188 the option without a space. `--no-gpg-sign` is useful to countermand 189 both `commit.gpgSign` configuration and earlier `--gpg-sign`. 190 191 `--empty=(drop|keep|abort)`:: 192 Control what happens when a commit becomes empty as a result of the 193 fixup. This can happen in two situations: 194 + 195 -- 196 * The fixup target itself becomes empty because the staged changes exactly 197 cancel out all changes introduced by that commit. 198 199 * A descendant commit becomes empty during replay because it introduced the 200 same change that was just fixed up into an ancestor. 201 -- 202 + 203 With `drop` (the default), empty commits are removed from the rewritten 204 history. Descendants of a dropped target commit are replayed directly onto 205 the target's parent. Note that dropping the root commit is not supported; 206 see LIMITATIONS. 207 + 208 With `keep`, empty commits are retained in the rewritten history as-is. 209 + 210 With `abort`, the command stops with an error if any commit would become 211 empty. 212 213 `--update-refs=(branches|head)`:: 214 Control which references will be updated by the command, if any. With 215 `branches`, all local branches that point to commits which are 216 descendants of the original commit will be rewritten. With `head`, only 217 the current `HEAD` reference will be rewritten. Defaults to `branches`. 218 219 EXAMPLES 220 -------- 221 222 Fixup a commit 223 ~~~~~~~~~~~~~~ 224 225 ---------- 226 $ git log --oneline --stat 227 `abc1234` (HEAD -> main) third 228 third.txt | 1 + 229 `def5678` second 230 second.txt | 1 + 231 ghi9012 first 232 first.txt | 1 + 233 234 $ echo "change" >>unrelated.txt 235 $ git add unrelated.txt 236 $ git history fixup ghi9012 237 238 $ git log --oneline --stat 239 jkl3456 (HEAD -> main) third 240 third.txt | 1 + 241 mno7890 second 242 second.txt | 1 + 243 pqr1234 first 244 first.txt | 1 + 245 unrelated.txt | 1 + 246 ---------- 247 248 The staged addition of `unrelated.txt` has been incorporated into the `first` 249 commit. All descendant commits have been replayed on top of the rewritten 250 history. 251 252 Drop a commit 253 ~~~~~~~~~~~~~ 254 255 ---------- 256 $ git log --oneline 257 `abc1234` (HEAD -> main) third 258 `def5678` second 259 ghi9012 first 260 261 $ git history drop 'main^{/second}' 262 263 $ git log --oneline 264 jkl3456 (HEAD -> main) third 265 ghi9012 first 266 ---------- 267 268 The `second` commit has been removed from the history, and `third` has been 269 replayed directly on top of `first`. All branches that pointed at the dropped 270 commit have been moved to its parent. 271 272 Split a commit 273 ~~~~~~~~~~~~~~ 274 275 ---------- 276 $ git log --stat --oneline 277 `3f81232` (HEAD -> main) original 278 bar | 1 + 279 foo | 1 + 280 2 files changed, 2 insertions(+) 281 282 $ git history split HEAD 283 diff --git a/bar b/bar 284 new file mode 100644 285 index `0000000`..`5716ca5` 286 --- /dev/null 287 +++ b/bar 288 @@ -0,0 +1 @@ 289 +bar 290 (1/1) Stage addition [y,n,q,a,d,p,?]? y 291 292 diff --git a/foo b/foo 293 new file mode 100644 294 index `0000000`..`257cc56` 295 --- /dev/null 296 +++ b/foo 297 @@ -0,0 +1 @@ 298 +foo 299 (1/1) Stage addition [y,n,q,a,d,p,?]? n 300 301 $ git log --stat --oneline 302 `7cebe64` (HEAD -> main) original 303 foo | 1 + 304 1 file changed, 1 insertion(+) 305 `d1582f3` split-out commit 306 bar | 1 + 307 1 file changed, 1 insertion(+) 308 ---------- 309 310 GIT 311 --- 312 Part of the linkgit:git[1] suite