1

I am using GitHub for the first time and I've spent past few days trying to wrap my head around basic git functionality and commands. Currently, my repository is private–I am the only person using it and pushing commits. However, I want to ensure that I am not creating a tangled web that will cause myself, or others, problems later on if I decide to make it public.

After making my first commit successfully, I realized I had left a proxy API key in my code, so I made an edit (I thought only in my local, but maybe I did in my remote as well) and quickly received a conflict message:

Your branch and 'origin/main' have diverged, 
and have 1 and 1 different commits each, respectively

After reading through SO to resolve the conflict, since the changes were insignificant, and I was unable to figure out a better alternative, I decided to use:

git fetch origin
git reset --hard origin/main

After getting my local back up-to-date with main, I still wanted to remove the sensitive information from my commit history entirely. So I once again made local changes to remove the sensitive code & since my repository only contained the initial commit, I staged the initial commit files and tried to amend it: git commit --amend --no-edit.

However, after running git status, I again received the same conflict message. At first I thought maybe git always shows a conflict until a commit is pushed and the files become up to date, but I don't believe thats the case, and I'm unsure of what I'm doing wrong.

After going to SO again, I came across multiple posts along the lines of the following: Local branch and remote branch have diverged after amending commit

So, following the post I used: git push --force-with-lease. This resolved the merge conflict, and correctly amended the changes to the initial commit, but now my repository shows:

"My real name" and "my GitHub username" initial commit                        "xxxxxx" 2 hours ago

I had changed my user email config using git config --local user.email (but not my name) after I made the very first initial commit (before resetting). However, I had expected the amended commit to only show the updated author.

Well that did not happen, so I spent more time researching, and per advice on similar questions, used: git commit --amend --no-edit --reset-author, and once again, now "your branch and main have diverged". I have spent a long while going through tons of posts and articles, and I don't understand why I keep getting this merge conflict error. No one else has edited any files, and after getting my branch up-to-date, I haven't done anything before entering those commands, yet each time, I end up in conflict?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
DAK
  • 116
  • 1
  • 10
  • 2
    Everytime you amend an already pushed commit you will end up in a state where the history of your local repository has diverged from the history of the remote repository. The first time you resolved it by reverting the state of your local repository. The second time you resolved it by forcing the state of the remote repository to that of the local repository. – mkrieger1 Apr 01 '23 at 22:43
  • @mkrieger1 Ok, that makes sense. So then, similarly to amend the author, I would have to again force push that change just like I did for the previous amendment, is that right? – DAK Apr 01 '23 at 22:52
  • If I understand correctly what you are trying to do, I think so, yes. – mkrieger1 Apr 01 '23 at 22:55
  • @mkrieger1 It did. Thank you, your explanation helped connect the dots that I had been skirting around. – DAK Apr 01 '23 at 23:00
  • Does this answer your question? [git merge conflict after git amend - "Your branch and 'origin/master' have diverged"](https://stackoverflow.com/questions/43289444/git-merge-conflict-after-git-amend-your-branch-and-origin-master-have-diver) – mkrieger1 Apr 01 '23 at 23:01
  • @mkrieger1 I don't believe so - functionally it broadly addresses "what happened", but I was trying to amend an existing pushed commit, not just get my local up-to-date with my main branch. Moreover, from that standpoint, I believe your first comment presented a clearer explanation. Further, my solution was to force push changes from local to amend the existing commit, which I also needed to do a second time to amend the author (to a single author). – DAK Apr 02 '23 at 15:15

1 Answers1

1

My old answer suggested a rebase to avoid force pushing but, in your case, you actually need to git push --force your local amended commit.

That will allow said amended commit to replace the remote one, ensuring the sensitive information is (almost) purged from the remote Git repository.
("almost", because the gc is not often run on a GitHub repository, and anyone knowing the old commit SHA1 could technically fetch it. But I suspect this is not an issue here)

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250