简体   繁体   中英

My local branch is not able to synch with master

I am working on local branch name is local1.I wrongly deleted one file which exists in master from my local branch and then pushed update to my local branch. While raising PR it is showing file is deleted. To resolve this issue i tried with git fetch /merge and git pull origin master but nothing is working. Now i am not able to synch my local branch to master.It is not updating.

Well, you told Git to delete the file and Git obliged. The commits in master / main didn't change anything with the file, so Git considers your version to be "newer". I recommend reading the Git Book for some additional background.

Now, back to your question. How can you restore the file? It depends a little on how you deleted it and if you are okay with throwing away your commits and re-creating new commits which look similar.

  1. You have a single commit which only deletes the file and changes nothing else.

    In that case, simply revert the commit: git revert $hash_of_your_commit . This will create a second, new commit, which undos the changes of your original commit (basically inverts/reverses the patch/diff).

    It will also work if your commit contains additional changes. These changes will be "undone" (ie reverted) too. The final result is as if the changes never happened (think: x+1-1 == x); even though the original commit is still there.

  2. You deleted the files and committed other changes at the same time.

    Use git checkout to get a known version of the file, eg from master or from your commit before you deleted it: git checkout master -- path/to/your/file , then create a new commit: git commit -m 'Restore deleted file' .

  3. Your history/branch is not shared and you are okay with rewriting it. Rewriting means throwing away all old commits and recreating new commits which look very similar, except for the file not being deleted.

    Please be aware of the implications of this actions, before you go this route.

    First, restore the file with regular means (eg option 1 or 2). Then use an interactive rebase and fuse the restoring commit into the original commit with squash or fixup : git rebase -i $hash_before_file_was_deleted .

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM