Bruk git rebase. Her er et eksempel:
Kode
git init
# to vanlige commits
touch superman.txt && git add superman.txt && git commit
touch batman.txt && git add batman.txt && git commit
# slett filene og commit med et uhell
mkdir backup && cp superman.txt batman.txt backup
git rm superman.txt batman.txt && git commit
# gjennoprett filene og commit
cp backup/* .
git add superman.txt batman.txt && git commit
# ny commit
touch aquaman.txt && git add aquaman.txt && git commit
# sjekk loggen for å finne ut hvor mange commits det er siden.
git log
# rebase de siste 5 commitene interaktivt
git rebase -i HEAD~5
Nå får du opp to filer etter hverandre. I den første endrer du "pick" til "squash" på commitene som skal kombineres med forrige commit.
Kode
1 pick b0d9203 legg til superman
2 squash 120e91b fjernet alt med et uhell
3 squash 8751f43 legg alt tilbake igjen
4 pick f55040a legg til aquaman
I den neste oppgir du hva den nye commit-meldingen blir. Her kommenterte jeg ut commitene jeg ikke vil ha. Om du ikke gjør dette blir alle meldingene kombinert inn i samme commit.
Kode
1 # This is a combination of 3 commits.
2 # The first commit's message is:
3 legg til superman
4
5 # This is the 2nd commit message:
6
7 # fjernet alt med et uhell
8
9 # This is the 3rd commit message:
10
11 # legg alt tilbake igjen
Nå gjenstår det å force-pushe. Hvis det er andre som bruker repositoriet må du avtale det med alle andre brukere, med mindre du er teit.
(Utestet, siden jeg ikke gadd)
Kode
git push origin master --force