Git雜記1:按怎刪除以前兮commit記錄?

介紹兩種:重鍊/變基。

重鍊(Reinitialize):完全删除以往所有commit記錄

罔摕家己無用兮repository做例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Clone your git repo
git clone https://github.com/unsioer/uselessgittest.git
# Enter your local repo
cd UselessGitTest
# Checkout
git checkout --orphan latest_branch
# Add all the files
git add -A
# Commit the changes
git commit -am "Reinitialize"
# Delete the branch
git branch -D master
# Rename the current branch to master
git branch -m master

上尾,汝只需要push

1
2
# Finally, force update your repository
git push -f origin master;

變基(Rebase):重整以往一寡commit記錄

若汝毋急於執行頂橛上尾彼句(執行矣也毋畏,再添加厘仔commit做試驗),來試看覓如何通過變基piàn-kirebase)囥汝以前commit兮烏歷史(XD),又欲保持其他commit記錄:

1
2
3
4
# Switch to `master` branch
git checkout master
# Find commit log. Press q to quit.
git log

選擇汝欲修改兮commit(s)之前的任意一條commit記錄:

1
2
# Rebase a commit
git rebase -i c8b2314c0b1768bc1443ae4cdb82ab29878a626e

咱來看跳出來兮git-rebase-todo,着是聽候汝處理這條commit之後所有commit記錄:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
pick 75198ef Update readme.md
pick c119c5b Update readme.md
pick ef9700f Update readme.md

# Rebase c8b2314..ef9700f onto c8b2314 (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

所以,若汝欲刪除以往一寡commits,可以改做squash合併,順紲可以rewordedit等等。

改好了後,push,收工:

1
2
# Finally, force update your repository
git push -f origin master

本站所有文章除特別聲明外,均採用 CC BY-SA 4.0 協議 ,轉載請註明出處!