0

I got two branches A, B.

In branch A, I added a file called a.py and consists:

import os
import math

I added and committed the file (and pushed to remote).

Than I created another branch, B like that:

git checkout -b B

in Branch B I added another file and added, committed and pushed it.

I also changed a.py to be like that:

import os
import sys

After that I also added 7 files and pushed them (in B).

Now, I regret and I want to keep the: import math by marging A to B without losing all my older commits. Only change back (but not manually) the import math back.

When I just use the IDE to merge A to B it says already up to date but it is not true because in A I have import math and in B I have import sys.

How can I fix it?

I'm afraid to lose all my job.

Thanks in advance.

Help Pleasee
  • 211
  • 3
  • 15

2 Answers2

4

Ipacheco is correct, but let me elaborate.

In git, when you merge two branches together, it doesn't consider what content is different. It considers what commits are different.

So you have branch A with import math. But more importantly you have commit #1.

When you branch from A to B, git copies commit #1 (with the import math change) to B. Then you make further changes -- commit #2. So B has a commit history of #1 and #2.

So when you try to merge A into B, git says that B already has commit #1 (you just overwrote it already). Essentially, A does not have any new work that B doesn't have. Thus B is "up to date".

If you want to go back, you actually have to revert the commits to B that have the changes you don't want.

EDIT: More specifically, you can use git revert <commit> from command line. This will add a new commit that undoes the specified commit. So you won't lose any work In between. But it does mean that both commits are in your history. (If that's a problem, look into rebase instead.) More on revert here. Use git log to find the commit hash.

Tracy Moody
  • 1,089
  • 6
  • 17
3

B is "already up to date" because it was a branch of A, and A wasn't updated after the B branch was created.

lpacheco
  • 976
  • 1
  • 14
  • 27