Git reset behavior

I have the following situation:

A stable version of the application has appeared in the master branch.

Developer Recently created a branch with branch-a functions with several commits (let them be a-1 , a-2 , a-3 ). The implemented functions here are based on the updated code from the wizard and are well tested at the moment.

Developer B had a branch function named branch-b with several commits (e.g. b-1 , b-2 , b-3 ). For some reason, Mr. B had an outdated version (based on the state of the owner a week or two ago) in his functional branch and did not test the code at all.

Both developers combined their function branches for management using:

  • git check wizard
  • git pull origin master
  • git merge branch-X (where X = a, b)
  • git push origin master

The rebase command was not used. This sequence was first performed by B, then by A.

When I (the C developer) pulled out of the wizard, I saw in git log something like:

  • a-merge: merge with the master using developer A
  • a-3
  • a-2
  • a-1
  • b-3 (yes, this fixation occurs immediately after the merger)
  • b-merge-conflict: merge with master developer B (thousands of file conflicts)
  • B-2
  • B-1
  • master-stable: previous stable commits

As a result, Mr. B somehow forced the old version of the code to overwrite the stable version when merging (as a result, b-merge-conflict commit was performed).

NOW I want to rewrite the story and save b-1 + b-3 + a-1 + a- 2 + a-3 and cancel b-2 , b-merge-conflict and a-merge .

My idea is to undo a few top commits to b-1 , and then use the cherry curl patch to apply b-3 , a-1 , a-2 , a-3 the new master commits.

BUT when I try:

git reset --hard HEAD ~ 7 I can see a history containing only old commits (until master stable), without the history of branches-a and branch-b.

When I try:

git reset --hard HEAD ~ 2

I see in history only a master-stable commit at the top, but NOT a-2 as I want.

It seems that git reset does not translate the digit after HEAD as the number of commits to reset (as I understood from the documentation), but as the number of HEAD changes to git pull (in my examples there are 2).

How can I correctly undo the first 7 commits of b-2 .. a-merge and rewrite history starting with b-1?

UPDATE set in comments

I used (without --all to exclude additional information)

git log --online --decorate --graph

* ef7d93f Merge with master by Developer A |\ | * 2b9dd31 b-4 | * 924a452 b-3 | * 1f9489d b-2 | * e3cd7a6 Merge by Developer B [2]: Merge branch 'master' from https://github.com .... | |\ | * | aece506 Merge by Developer B [1]: merge branch | * | 487e7ee b-1 * | | d9404f8 a-1 | |/ |/| * | 9b202ce master-stable last commit 
+6
source share
2 answers

git log lying to you. It presents the Git story, as if it were linear, it showed you commits in date order. This is not very helpful. git log --graph --decorate will give you a clearer history by showing you the tree (the graph is actually) of the commits. From what I can decide, your repository is as follows.

  a1 - a2 - a3 / \ origin c1 - c2 - c3 - c4 - b-merge - b3 -------- a-merge [master] \ / b1 -------------b2 

As you can see, “returning to seven commits” can have several interpretations. That's why you should avoid these notations to move more than a few commits backward and refer to commit identifiers instead.

What you want is this.

  a1 - a2 - a3 [branch-a] / c1 - c2 - c3 - c4 [master] \ b1 - b3 [branch-b] 

To get there, create branches A and B c4 so that you have a place to create.

 git branch branch-a c4 git branch branch-b c4 [branch-b] a1 - a2 - a3 [branch-a] / \ c1 - c2 - c3 - c4 - b-merge - b3 -------- a-merge [master] \ / b1 -------------b2 

Now check out these branches and cherries to make the appropriate changes to them, correcting any conflicts.

  b1b - b3b [branch B] / | a1a - a2a - a3a [branch A] | / | / a1 - a2 - a3 |/ / \ c1 - c2 - c3 - c4 - b-merge - b3 -------- a-merge [master] \ / b1 -------------b2 

This may seem messy, but now the checkout wizard and git reset --hard c4 , and all the clutter surviving from the a-merge host will go away (that white lie, start / master will keep it visible until you click, also Git doesn’t actually throw out commits in a few weeks).

  b1b - b3b [branch B] / | a1a - a2a - a3a [branch A] |/ c1 - c2 - c3 - c4 [master] 

Now you can combine A and B as a rule. When you are done, you need push --force because the master is not a descendant of the original / master.

This is just one way to accomplish what you want. It is important to be able to visualize the repository graph, where you want, and what commands will convert it.

+2
source

I am not sure about HEAD~ because I usually use HEAD^ .

However, you should not use this notation. You can simply put the hexadecimal SHA-1 commit hash or the first 7 or so digits.

 git reset --hard 72abfd4 
+1
source

Source: https://habr.com/ru/post/982825/


All Articles