1

I have a problem to solve on Git , here are the sequence of steps we i perform

Let say i make 3 commits on master

- commit-1
- commit-2
- commit-3

Now i checkout to commit-2

git checkout commit-2

then i make commit-4

Now i want to retain all four commits on the master branch and my master branch's git log should look in the following order

Commit-3
commit-4
commit-2
commit-1

How do i achieve it ?

Karthic Rao
  • 3,624
  • 8
  • 30
  • 44

2 Answers2

1

I would do it as follows

first you commit commits 1, 2, 3

git commit -am "commit 1"
git commit -am "commit 2"
git commit -am "commit 3"

then you go back to commit 2

git checkout <<hash or HEAD^>

then you commit commit 4

git commit -am "commit 4"

then you merge your detached head into new head

git merge <<reference of commit 3>>

please comment out if there is a better way of doing it

cerkiewny
  • 2,761
  • 18
  • 36
  • merge would create merge conflict and i could commit only one of the version . what if lets say im just working on one file and i want all snapshots of the change without having to solve merge conflicts – Karthic Rao Mar 25 '15 at 15:52
1

As soon as you checkout commit-2, you are in a detached HEAD mode.

The first things to do is to create a branch where your commit-4 is:

git checkout commit-2
git add ...
git commit -m "commit-4"
git checkout -b tmp

1--2--3 (master) 
    \
     4 (tmp) 

Then you can rebase master on top of it in order to replay commit-3 on top of commit-4

git rebase tmp master

1--2--4--3 (master, tmp)
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • But this has put commit 4 over commit 3 . But i want all the commits on the new branch(tmp) on top of the checked out commit (commit-2), but below the commits above the initally checked out commit(commit-3) , that is , i want commit-3 on top of commit-4 – Karthic Rao Mar 25 '15 at 15:56
  • @KarthicRao no it doesn't put 4 over 3: it put 3 (master) on top of tmp (4) – VonC Mar 25 '15 at 16:09
  • Thank you , I had checked out tmp first and then made commit 4 . This works :) – Karthic Rao Mar 26 '15 at 03:20