interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / Documentation / git-history.adoc

312 lines · 11603 bytes Raw Blame History
1git-history(1)
2==============
3
4NAME
5----
6git-history - EXPERIMENTAL: Rewrite history
7
8SYNOPSIS
9--------
10[synopsis]
11git history drop <commit> [--dry-run] [--update-refs=(branches|head)] [--empty=(drop|keep|abort)] [--[no-]gpg-sign[=<key-id>]]
12git history fixup <commit> [--dry-run] [--update-refs=(branches|head)] [--reedit-message] [--empty=(drop|keep|abort)] [--[no-]gpg-sign[=<key-id>]]
13git history reword <commit> [--dry-run] [--update-refs=(branches|head)] [--[no-]gpg-sign[=<key-id>]]
14git history split <commit> [--dry-run] [--update-refs=(branches|head)] [--[no-]gpg-sign[=<key-id>]] [--] [<pathspec>...]
15git history squash [--dry-run] [--update-refs=(branches|head)] [--[no-]edit] [--[no-]gpg-sign[=<key-id>]] <revision-range>
16
17DESCRIPTION
18-----------
19
20Rewrite history by rearranging or modifying specific commits in the
21history.
22
23THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.
24
25This command is related to linkgit:git-rebase[1] in that both commands can be
26used 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
36Overall, linkgit:git-history[1] aims to provide a more opinionated way to modify
37your commit history that is simpler to use compared to linkgit:git-rebase[1] in
38general.
39
40Use linkgit:git-rebase[1] if you want to reapply a range of commits onto a
41different base, or interactive rebases if you want to edit a range of commits
42at once.
43
44LIMITATIONS
45-----------
46
47This command does not (yet) replay merge commits onto the rewritten
48history: if a commit that would be replayed is a merge, the operation is
49rejected, and you should use linkgit:git-rebase[1] with the
50`--rebase-merges` flag instead. The `squash` subcommand can still fold merges
51that lie inside the selected range, subject to the restrictions below.
52
53Furthermore, the command does not support operations that can result in merge
54conflicts. This limitation is by design as history rewrites are not intended to
55be stateful operations. The limitation can be lifted once (if) Git learns about
56first-class conflicts.
57
58When using `fixup` with `--empty=drop`, dropping the root commit is not yet
59supported. Likewise, `drop` cannot remove the root commit or a merge commit.
60
61COMMANDS
62--------
63
64The following commands are available to rewrite history in different ways:
65
66`drop <commit>`::
67 Remove the specified commit from the history. All descendants of the
68 commit are replayed directly onto its parent.
69+
70The root commit cannot be dropped as that may lead to edge cases where refs
71end up with no commits anymore. Merge commits cannot be dropped either; see
72LIMITATIONS.
73+
74If `HEAD` points at a commit that is to be rewritten, the index and working
75tree are updated to match the new `HEAD`. The command aborts before any
76references are updated in case local modifications would be overwritten.
77+
78If replaying any descendant would result in a conflict, the command aborts
79with an error.
80
81`fixup <commit>`::
82 Apply the currently staged changes to the specified commit. This is
83 similar in nature to `git commit --fixup=<commit>` followed by `git
84 rebase --autosquash <commit>~`. 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+
88The commit message and authorship of the target commit are preserved by
89default, unless you specify `--reedit-message`.
90+
91If applying the staged changes would result in a conflict, the command
92aborts with an error. All branches that are descendants of the original
93commit are updated to point to the rewritten history.
94
95`reword <commit>`::
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 <commit> [--] [<pathspec>...]`::
101 Interactively split up <commit> 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+
108The commit messages of the split-up commits will be asked for by launching
109the configured editor. Authorship of the commit will be the same as for the
110original commit.
111+
112If passed, _<pathspec>_ can be used to limit which changes shall be split out
113of the original commit. Files not matching any of the pathspecs will remain
114part of the original commit. For more details, see the 'pathspec' entry in
115linkgit:gitglossary[7].
116+
117It is invalid to select either all or no hunks, as that would lead to
118one of the commits becoming empty.
119
120`squash <revision-range>`::
121 Fold all commits in _<revision-range>_ 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+
127The range is given in the usual `<base>..<tip>` form, where _<base>_ is
128the commit just below the oldest commit to squash. For example, `git
129history squash HEAD~3..HEAD` folds the three most recent commits into
130one, and `git history squash HEAD~5..HEAD~2` squashes an interior range
131while leaving the two newest commits in place. Several revisions may be
132given, for example `HEAD~3..HEAD ^topic` to additionally exclude what is
133already on `topic`. Rev-list options may also be given, but any that would
134change how the range is walked are overridden with a warning.
135+
136An editor opens pre-filled with the messages of all the folded commits so you
137can combine them. With `--no-edit`, the oldest commit's message is preserved
138instead, except that an `amend!` commit targeting it replaces its message.
139+
140The selected commits must form a connected graph with a single tip and must
141not include a root commit. Every parent of a commit after the oldest one must
142either be selected or also be a parent of the oldest commit. When the oldest
143commit is a merge, all of its parents are preserved in the squashed commit.
144+
145A `fixup!`, `squash!`, or `amend!` commit is refused unless the commit it
146targets is also in the range, so the fold does not silently absorb a
147marker meant for a commit outside it. As an exception, a range made up entirely
148of markers for one target is combined into a single commit. With `--no-edit`,
149the last `amend!` message is used if there is one; a `squash!` or `amend!` is
150otherwise refused if folding it would discard its message.
151+
152The editor template mirrors `git rebase -i --autosquash`: each `fixup!`,
153`squash!`, or `amend!` is grouped under the commit it targets rather than
154shown in commit order. A `fixup!` message is dropped (commented out in full),
155a `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
157target first, in which case it keeps its body like a `squash!`.
158+
159A local branch descended from a selected commit but not from the range tip
160cannot be rewritten as a descendant of the result, so with the default
161`--update-refs=branches` the command refuses. Rerun with `--update-refs=head`
162to rewrite only the current branch and leave such branches unchanged. Tags
163and remote-tracking refs are always left unchanged.
164
165OPTIONS
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[<key-id>]`::
184`--gpg-sign[=<key-id>]`::
185`--no-gpg-sign`::
186 GPG-sign rewritten commits. The _<key-id>_ 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+
203With `drop` (the default), empty commits are removed from the rewritten
204history. Descendants of a dropped target commit are replayed directly onto
205the target's parent. Note that dropping the root commit is not supported;
206see LIMITATIONS.
207+
208With `keep`, empty commits are retained in the rewritten history as-is.
209+
210With `abort`, the command stops with an error if any commit would become
211empty.
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
219EXAMPLES
220--------
221
222Fixup a commit
223~~~~~~~~~~~~~~
224
225----------
226$ git log --oneline --stat
227abc1234 (HEAD -> main) third
228 third.txt | 1 +
229def5678 second
230 second.txt | 1 +
231ghi9012 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
239jkl3456 (HEAD -> main) third
240 third.txt | 1 +
241mno7890 second
242 second.txt | 1 +
243pqr1234 first
244 first.txt | 1 +
245 unrelated.txt | 1 +
246----------
247
248The staged addition of `unrelated.txt` has been incorporated into the `first`
249commit. All descendant commits have been replayed on top of the rewritten
250history.
251
252Drop a commit
253~~~~~~~~~~~~~
254
255----------
256$ git log --oneline
257abc1234 (HEAD -> main) third
258def5678 second
259ghi9012 first
260
261$ git history drop 'main^{/second}'
262
263$ git log --oneline
264jkl3456 (HEAD -> main) third
265ghi9012 first
266----------
267
268The `second` commit has been removed from the history, and `third` has been
269replayed directly on top of `first`. All branches that pointed at the dropped
270commit have been moved to its parent.
271
272Split a commit
273~~~~~~~~~~~~~~
274
275----------
276$ git log --stat --oneline
2773f81232 (HEAD -> main) original
278 bar | 1 +
279 foo | 1 +
280 2 files changed, 2 insertions(+)
281
282$ git history split HEAD
283diff --git a/bar b/bar
284new file mode 100644
285index 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
292diff --git a/foo b/foo
293new file mode 100644
294index 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
3027cebe64 (HEAD -> main) original
303 foo | 1 +
304 1 file changed, 1 insertion(+)
305d1582f3 split-out commit
306 bar | 1 +
307 1 file changed, 1 insertion(+)
308----------
309
310GIT
311---
312Part of the linkgit:git[1] suite