Devolus
Devolus

Reputation: 22094

How to pull a git branch when the history has been altered?

I created a branch on my master, and applied several commits. After I'm done with all changes, I rebase and squash all these little commits with no meaning (i.e. fix typo, etc.). Because of the rebase I have to force push the branch. So far everything works fine.

Now when I go to the other machine, where I have the same repository, I want to pull the latest version, but this is always giving me errors and I end up by cloning the repository again.

 Machine1             Machine2
    |                    |
 master N             master N
    |                    |
    + Branch1            + Branch1
        |                    |
      Commit1                |
        |                    |
      Commit2                |
        |                    |
      Commit3                |
        |                    |
      Rebase                 |
        |                    |
     Force Push              |
                         Pull error

Is there some way to properly update this on the other machine, without having to rebase again or cloning?

Upvotes: 1

Views: 790

Answers (2)

MrTux
MrTux

Reputation: 34002

If the branch on your second machine is "the same" and you don't have any uncommitted changes, you can easily hard reset the local branch to the pulled branch - that it will be exactly the same afterwards: Open the log, make sure you are on the correct branch, select the remote tracking branch (you might have to enable "Show all branches" in the lower left), open the context menu and perform a hard reset.

  • If you have uncommitted changes stage them first.
  • If you have commits that are not yet pushed, those might also be lost. So take care from them, e.g. by pushing/rebasing them first or create a new branch so that you can later charry pick or merge then.

Upvotes: 2

ruohola
ruohola

Reputation: 24087

Most often just:

git pull --rebase

works.

If it causes some issues and you truly want to make Machine2 match the remote and Machine1 completely, you can use:

git reset --hard origin/Branch1

Make sure you don't have any uncommited changes on Machine2 though.

Upvotes: 3

Related Questions