Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stash the changes between local repository and remote repository

Tags:

git

github

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?

like image 805
Nu2Overflow Avatar asked Oct 11 '15 20:10

Nu2Overflow


People also ask

How do you discard local changes and pulls from a remote?

There are two Git commands a developer must use in order to discard all local changes in Git, remove all uncommited changes and revert their Git working tree back to the state it was in when the last commit took place. The commands to discard all local changes in Git are: git reset –hard. git clean -fxd.

How do you stash part of changes?

The easiest way to create a git stash is to simply run the “git stash” command without any parameters. As a consequence, all the changes staged for commit in your current working directory will be saved on the side for later use.

Which command brings the changes from remote repository to local repository?

The git fetch command downloads commits, files, and refs from a remote repository into your local repo.


2 Answers

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.
like image 79
Schwern Avatar answered Oct 16 '22 15:10

Schwern


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

like image 24
Chad Bingham Avatar answered Oct 16 '22 16:10

Chad Bingham