4

I have made 10 commits and each commit have been already pushed to central repository. Lets suppose i have commits for example.

C1-C2-C3-C4-C5

where C5 was my last commit and all commits have been pushed. The branch i master. I want to delete the history and want to keep my last commit in git logs. I don't want anyone can reset head to any previous commit or push.

someone's told me to use git rebase -i head~4 but i want to make my sense clear, before implementing it.

I have implement following commands

git checkout --orphan newB
git add -A
git commit
git branch -D master
git branch -m master
git push

and after these above commands my git graph become as per given image below

git graph

Current graph of my repository is look like this

enter image description here

Look the first two commits and it is detached from master. And i want t go to the encircled hash. git rebase giving me following error

$ git rebase -i head~5
fatal: Needed a single revision
invalid upstream head~5
3
  • 'git rebase' is the right answer. Commented Mar 4, 2016 at 10:51
  • how can i use git rebase Commented Mar 4, 2016 at 10:53
  • @ASyedFahad firs, you need to be onto the same branch where you're trying to rebase on, second, you should use head~<n> where <n> is number of commits behind this Commented Mar 4, 2016 at 11:18

2 Answers 2

3

To rewrite your first commit, you should not specify an upstream commit. (The first upstream commit you could provide is your first commit, which would begin rewriting from the first commit, instead of including the first commit.)

Instead, you can use --root which indicates that you want to begin rewriting from the root of the branch, which will allow you to rewrite the first commit.

For example:

% git log --oneline
072c8fc Modification 3 to master
9bdb3a2 Modification 2 to master
45f0758 Modification 1 to master
96da320 Initial revision

Specifying an upstream of 96da320 (or HEAD~3) will not allow you to rewrite the first commit. Similarly, you cannot specify HEAD~4 since that would refer to your first commit's parent, which of course does not exist.

Instead, if you use the --root flag:

% git rebase -i --root

Then your interactive rebase script will contain the root commit:

pick 96da320 Initial revision
pick 45f0758 Modification 1 to master
pick 9bdb3a2 Modification 2 to master
pick 072c8fc Modification 3 to master
Sign up to request clarification or add additional context in comments.

Comments

1

Use git rebase -i head~5 instead of git rebase -i head~4


What rebase is used for?

It's used to apply commits on top of history but, in some case (such this) is used also to rewrite the history


What is -i flag?

It is used to start an interactive process where an editor (such vi) will be showed to you and where you can choose from different options


Why head~5

Because you need to rewrite history for last five commits


What should I do next?

With interactive rebasing, something like this will be showed to you

pick 9e40be3 First commit message
pick 6e20a2a Second commit message
pick 0b06199 Third commit message
pick 3b4d266 Fourth commit message
pick c273e2f Fifth commit message

and below

# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell

where c273e2f (for instance) is the hash that identify a commit.

Now you can choose what to do with your commits. If you need first four commits to be only one, you can apply either a fixup or a squash (see commands above).

With fixup you won't be able to "travel" commits messages and only first commit message (or to be honest the commit message for commit you're fixing up) will be taken.

With squash you can choose what message to keep and even edit them (another "editor page" will be showed to you if this is chosen methodology).

A working example for you could be something like

pick 9e40be3 First commit message
f 6e20a2a Second commit message
f 0b06199 Third commit message
f 3b4d266 Fourth commit message
pick c273e2f Fifth commit message

This will result in two commits (first with second-third-fourth "merged" and fifth) where first commit will have first original commit message whether fifth commit will have fifth original commit message


Pay attention

As you are going to rewrite history, when you push, you need to specify -f as flag and if you're doing this in a "public" branch (such dev, or master, or whatever) this could be a pain in the back for who needs to push to that branch aswell.

2 Comments

HEAD~5 would refer to a non-existent commit since your first commit does not have a parent.
@EdwardThomson: i perfectly know but OP's edited question AFTER my answer and now I don't have time to edit it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.