You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/07-git-tools/sections/replace.asc
+10-5Lines changed: 10 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,7 +29,8 @@ We want to break this up into two lines of history.
29
29
One line goes from commit one to commit four - that will be the historical one.
30
30
The second line will just be commits four and five - that will be the recent history.
31
31
32
-
image::images/replace1.png[]
32
+
.Example Git history
33
+
image::images/replace1.png[Example Git history]
33
34
34
35
Well, creating the historical history is easy, we can just put a branch in the history and then push that branch to the `master` branch of a new remote repository.
35
36
@@ -44,7 +45,8 @@ c6e1e95 (history) Fourth commit
44
45
c1822cf First commit
45
46
----
46
47
47
-
image::images/replace2.png[]
48
+
.Creating a new `history` branch
49
+
image::images/replace2.png[Creating a new `history` branch]
48
50
49
51
Now we can push the new `history` branch to the `master` branch of our new repository:
50
52
@@ -97,7 +99,8 @@ On occasions when we're doing weirder things like this, they allow us to do real
97
99
You can read more about plumbing commands in <<ch10-git-internals#_plumbing_porcelain>>.
98
100
=====
99
101
100
-
image::images/replace3.png[]
102
+
.Creating a base commit using `commit-tree`
103
+
image::images/replace3.png[Creating a base commit using `commit-tree`]
101
104
102
105
OK, so now that we have a base commit, we can rebase the rest of our history on top of that with `git rebase --onto`.
103
106
The `--onto` argument will be the SHA-1 we just got back from `commit-tree` and the rebase point will be the third commit (the parent of the first commit we want to keep, `9c68fdc`):
@@ -110,7 +113,8 @@ Applying: fourth commit
110
113
Applying: fifth commit
111
114
----
112
115
113
-
image::images/replace4.png[]
116
+
.Rebasing the history on top of the base commit
117
+
image::images/replace4.png[Rebasing the history on top of the base commit]
114
118
115
119
OK, so now we've re-written our recent history on top of a throw away base commit that now has instructions in it on how to reconstitute the entire history if we wanted to.
116
120
We can push that new history to a new project and now when people clone that repository, they will only see the most recent two commits and then a base commit with instructions.
@@ -172,7 +176,8 @@ c1822cf First commit
172
176
173
177
Cool, right? Without having to change all the SHA-1s upstream, we were able to replace one commit in our history with an entirely different commit and all the normal tools (`bisect`, `blame`, etc) will work how we would expect them to.
174
178
175
-
image::images/replace5.png[]
179
+
.Combining the commits with `git replace`
180
+
image::images/replace5.png[Combining the commits with `git replace`]
176
181
177
182
Interestingly, it still shows `81a708d` as the SHA-1, even though it's actually using the `c6e1e95` commit data that we replaced it with.
178
183
Even if you run a command like `cat-file`, it will show you the replaced data:
0 commit comments