1

Here is my current commits.

commit1 - commit 2 - commit3 - commit4 - cmmit5 (my-app branch)

I checked out commit1 and made changes

git checkout <commi1's SHA>
// made some changes
git add . && git commit -m "commit prime 1"
// made other changes
git add . && git commit -m "commit prime 2"

Now, I want to merge(?) or rebase(?) on the top of my-app branch.

I don't care about commit 5, I just want to put my currently changed commit1 to the top of my-app branch (not merging but overriding!). So the output will be below

commit1 - commit 2 - commit3 - commit4 - cmmit5 - commit prime 1 - commit prime2" (my-app branch)
  • I don't think I need to "merge" commit 5 and commit prime 2.
  • Maybe should I use rebase?

Here is my current terminal prompt:

jbaaa at jo's computer ~/directory on (HEAD detached from 8a36f955fc)*

git status output:

HEAD detached from 8a36f955fc

nothing to commit, working tree clean.

Suppose commit5 is

'12345'
''
''

commit1 is

'1'
''
''

commit prime2 is

'1' 
'prime1 and prime2'

Final result must be

'1'
'prime1 and prime2'

If we merge, we're getting this result instead. How do we avoid it?

'12345'
'prime1 and prime2'
merry-go-round
  • 4,533
  • 10
  • 54
  • 102

2 Answers2

4

You are in a so called "HEAD detached" state, meaning your not working on a branch. First, put your current work into a branch.

git checkout -b my-feature

If you want to drop commit 5, rebase your work onto commit 4.

git rebase <commit4's SHA>

What you get will look like this:

c1 - c2 - c3 - c4 - c5 (my-app)
                \
                 p1 - p2 (my-feature)

As you don't care about commit 5, reset my-app to my-feature (p2).

git checkout my-app
git reset --hard my-feature

Final result:

                 c5 # no branch
                /
c1 - c2 - c3 - c4 - p1 - p2 (my-app, my-feature)

I'm afraid to use rebase

If you are not sure about rebasing, leave your current branch untouched and create a new branch before you start to rebase, eg:

git checkout -b my-feature-rebased

Final result:

                 c5 # no branch
                /
c1 - c2 - c3 - c4 - p1 - p2 (my-app, my-feature-rebased)
 \
  p1 - p2 (my-feature) # backup
sergej
  • 17,147
  • 6
  • 52
  • 89
-1

you can use git rest --hard <commit1 hash> to move your head to commit1