1

Git Structure:

1. Local repo:
    (1) main
    (2) test_branch

2. Remote repo:
    (1) main
    (2) test_branch

For some reasons, some contents in test_branch in the local repo are messed up, so I want to pull the test_branch in the remote repo to the test_branch in the local repo to recover the original structure.

Tried:

git checkout test_branch

git pull origin test_branch

But it didn't work.

Also dig other threads :

Git: How do I force "git pull" to overwrite local files?

Tried:

git fetch --all

git branch backup-test_branch

git reset --hard origin/test_branch

But it also didn't work.

Any suggestions are appreciated.

Sky
  • 43
  • 5
  • `git fetch --all` might not work as you expect. Try `git branch backup-master;git fetch origin test_branch;git reset --hard FETCH_HEAD`. – ElpieKay Oct 15 '21 at 02:24

1 Answers1

1

You can always delete your local branch and recover from the remote, try the following:

git checkout main

git branch -D test_branch

git checkout test_branch

To explain what it's doing, it is changing the current branch to main, deleting the local copy of the test_branch and then changing back to the test_branch branch, but now synchronized with the remote one.

Bernardo Duarte
  • 4,074
  • 4
  • 19
  • 34
  • After deleting test_branch, shouldn't it add -b as checkout test_branch? – Sky Oct 15 '21 at 08:29
  • @Sky If you add the `-b` flag then you're going to create a new branch, if you don't then you're going to create a copy of the remote `test_branch`. That happens because you have already fetched from your remote and you have a local reference to a remote branch. – Bernardo Duarte Oct 15 '21 at 13:27
  • IIUC, do you mean that `git checkout test_branch` will create a copy of the remote `test_branch`? As my understanding, copying a remote repository generally uses the command such as `git fetch` or `git pull`, but here I don't see such command in the above three commands. That's why I am confused. – Sky Oct 16 '21 at 11:23
  • @Sky You're right, git checkout wont create a copy, but It will use the already created copy from a previous `git pull` or `git fetch`. – Bernardo Duarte Oct 16 '21 at 18:14
  • Got it. Thank you very much! – Sky Oct 17 '21 at 00:53