1

Recently I had to squash commits due to several bad commits. Simply doing git pull --rebase origin master worked for me but my teammate already had merged new history on top of old history. So I want to know is if it is possible to overwrite local history?

Let's say there are three timelines in branches remoteNew, localOld and localNew has hour old commit.

Following works and completely overwrites old history with new history. Though thing to note here is, oldHistory had no new commits after newHistory was created.

git checkout localOld
git pull --rebase origin remoteNew

Following does not work as I would want. Instead of overwriting local history, it creates a merge with conflicts.

git checkout localNew
git pull --rebase origin remoteNew

I believe this is happening because local has new commit. Though what I really want is git push --force like result but for pull. Other way would be to just replace master branch with new branch and rename it. Any other option to overwrite local history?

Note: Would like to clear that this is old repository but team isn't working on it yet, we are in initial stage of adopting git and finalizing workflow before entire team starts working on it.

chadedgar
  • 111
  • 1
  • 6
  • I am unclear on what you are wanting to accomplish here. Do you want to keep the commit which exists in the branch localNew? Or are you trying to get rid of the commit which is in localNew? Is the difference between localOld and localNew just that one commit? Is remoteNew really a unique branch name? Or is it the remote version of a branch which you have locally? What you want to do can probably be easily accomplished, but first we need to understand the question. – Randy Leberknight Jun 06 '21 at 21:07

2 Answers2

5

To overwrite local history with remote history, will remove all files, where are only on local history. So be carefully!

git fetch --all
git reset --hard <remote>/<branch-name>

So with git fetch you download the latest remote history. With git reset you reset the branch to that what you just fetched.

For more information, look at this similar question: How do I force "git pull" to overwrite local files?

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
  • 2
    Just `fetch`, no need for `fetch --all` – matt Jun 06 '21 at 21:36
  • 1
    Thanks! @matt true, that question actually came in results when I tried to search for answer but I never checked it because it says "overwrite local files" rather than history. – chadedgar Jun 06 '21 at 21:42
  • 1
    @matt this isn't the same answer! The commands are given from **Git** and my explanation have I wrote by myself... – SwissCodeMen Jun 07 '21 at 07:39
1

git pull --rebase ... will run a rebase of your work on top of the remote branch ; a rebase may well trigger conflicts, and you will need to fix them if you want to proceed.

As long as the rebase isn't completed, localBranch will still point to your branch unmodified, you can inspect the history of localBranch and origin/remoteBranch to see what changes have landed on the remote, and may potentially conflict with your local changes :

git log --graph --oneline localBranch origin/remoteBranch

If you want to complete rebasing your local work on top of the remote branch :

  • inspect and fix the conflicts in conflicting files,
  • run git rebase --continue to proceed

If you want to cancel the rebase :

  • run git rebase --abort

In any case, the remote branch is now updated.

If you canceled the rebase, you may re-run git rebase origin/remoteBranch from your localBranch at any time.

LeGEC
  • 46,477
  • 5
  • 57
  • 104