How to stash the changes between local repository and remote repository

I want to stash all the changes between local repository and remote origin/master. There are some commits made in the local repository at the same time some other changes made in remote repository.

  • I want to stash the differences between local and remote.
  • Discard all the local commits
  • Bring the local repository to in sync with remote.
  • Apply my stash changes.

How can I achieve this using git commands?

  • Why doesn't `git checkout` automatically do `git submodule update --recursive`?
  • How do I install and use MySQLdb for Python 3 on Windows 10?
  • Undoing commits that have been pushed to remote repo
  • How do I configure Git to pull from one repo and push to another?
  • Managing a set of related repositories relative in Git
  • How to install Git with PCRE support on OSX with homebrew?
  • Remove last pull github
  • What is wrong with my git 1.8.4.2-1?
  • Issue with uname when trying to launch atom from MSYS shell
  • How can I commit the current state of my repo as master?
  • One project, Multiple customers with git?
  • git - trim whitespace
  • 2 Solutions collect form web for “How to stash the changes between local repository and remote repository”

    I want to stash all the changes between local repository and remote origin/master.

    “stash” has a special meaning in Git, git stash puts uncommitted changes in a special commit for retrieval later. It’s used when you have some work that’s not ready to be committed, but you need to do something to the repository like checkout another branch or push or pull. For the rest of this answer I will use “stash” to mean git stash.

    There are some commits made in the local repository at the same time some other changes made in remote repository.

    This is situation normal.

    I want to stash the differences between local and remote.
    Discard all the local commits

    These steps are not necessary. Git can take care of this. See below.

    Bring the local repository to in sync with remote.

    You get an up to date version of the remote repository with git fetch origin (assuming the remote is named origin). This is safe and will not modify any local branches.

    Apply my stash changes.

    There’s no need to move your local commits out of the way. Git can merge your local work with the remotes. However if you have uncommitted work you’re going to want to stash it. git stash save will squirrel your uncommitted work away for later.

    After fetching you can git merge origin/master to merge your work with the remote or git rebase origin/master to put all your changes on top of the remote.

    You can do the fetch and merge/rebase in a single command. To fetch and merge use git pull origin master. To fetch and rebase use git pull --rebase origin master.

    Now that you’re up to date you can git stash pop to restore your stashed changes.

    git pull --rebase is the normal way you should be updating your local branches with work from the remote. A lot of merging that’s nothing but updating the remote will result in a confused history, rebasing keeps everything nice and linear. You don’t need to tell it the remote and branch, Git can usually figure this out for you. The normal workflow looks like this.

    • git stash save if you have uncommitted work.
    • git pull --rebase to get updates from the remote and apply them
    • git stash pop to restore your uncommitted work.

    git stash will stash your changes in a dirty working directory

    git stash pop Remove a single stashed state from the stash list and apply it on top of the current working tree state.

    git diff master will show the differences.

    To see more stash options: git stash --help

    EDIT

    Edit made for your revised question:

    1. I want to stash the differences between local and remote.

    2. Discard all the local commits

    3. Bring the local repository to in sync with remote.

    4. Apply my stash changes.

    First of all, number 2 will conflict with number 4. So I will explain how to update with remote by stashing:

    Stash your changes. This will remove them from your current working directory.
    git stash

    Pull your changes from remote
    git pull origin <branch>

    Pop your changes back.
    git stash pop

    NOTE: I recommend having a merge tool installed for this incase you run in to merge conflicts. I prefer P4Merge. If you get merge conflicts:
    git mergetool

    If you are wanting to know how to do these individually:

    1. I want to stash the differences between local and remote.
      git stash

    2. Discard all the local commits
      I just do git stash and forget about my stash. I don’t know if this is preferred or not. I think you can “drop” your stash change if you need.

    3. Bring the local repository to in sync with remote.
      git stash
      git pull origin <branch>
      git stash pop`

    4. Apply my stash changes.
      If you want to keep your stash on the stash list:
      git stash apply
      if you want to remove the stash from the stash list:
      git stash pop

    And your all set. Hope this helps

    Git Baby is a git and github fan, let's start git clone.