How do I go to the last commit from remote / master in git?

I accidentally saved the file on the wrong branch. Then I did git reset --soft HEAD^

and now git status

says I am 14 commits behind and lists a bunch of modified files in red. Including some untraceable files.

I have no idea what to do. I tried to go to the last commit from the remote repo, but it says my local changes will be overwritten after I do git pull

.

Reference. I don't want to mess up my local repo anymore. I just want to go back to the state I was in, save the edited file to the correct branch, and commit.

+3


source to share


2 answers


If you've changed your local branch and don't want to lose any changes, you can use git -stash . Before any attempt, I recommend that you back up your local repository . Follow these steps:



  • git stash save 'local changes'

    ... This will save your local changes to a temporary location and allow you to work on the current branch without any of those previous changes.

  • git pull <remote-name> <branch-name>

    ... As you already know, it will fetch new commits from the remote branch and apply to your local branch.

  • git stash pop

    ... This will revert what you did in step 1. It will apply your previous changes (the ones you saved) to the local branch. You are responsible for resolving any conflicts, if any. Once you're done, you might want to commit and click on it.

+3


source


Do not panic



  • Backing up your repository.

  • Replace the changes with git stash save

    . This will put your changes into stash .

  • Then pull out the remote branch: git pull <remote> <wrong-branch>

    . This will revert your working copy to the original HEAD branch. This is what your branch looked like before you started working on the changeset.

  • Now apply the hidden changes to your working copy: git stash apply

    . Git will be smart enough to ignore hidden changes that match existing commits, so you will be left with your working copy in the same state as before you made a commit on the wrong branch.

  • Now check out the correct branch and run as usual:

    git checkout <right-branch>
    git add [...]
    git commit -m "blah blah blah"
    
          

    You may need to reject your changes if you check out the correct branch, if your changeset is incompatible with the correct branch, but after that your changes will now be committed to the right branch.

+1


source







All Articles