Restoring Lost Remote Tracking to a Local Git Branch

Today, I ran into an issue where my local Git repository lost knowledge of the remote branch it is tracking.  I had already made local commits to this branch and I did not want to mess with the log history or end up causing any confusion creating artificial merge records. Here is what I ended up doing (I am really sharing this for my future reference and in hope it helps someone):

1-git reset –soft <last-commit-before my local changes>

2-git stash save all my local changes

3- git branch –set-upstream-to=origin/<remote branch> <my local branch>

3-git reset HARD origin/<branch name>

4-git stash apply stash@{0}

5- git add .

6-git commit –m “all my local changes”

7-git push


Links for reference:

-Use git Stash Apply:

-Understand resetting your commit HEAD (can be done for local commits or remote commits)

-Know the difference between soft and hard reset