3

How can I pull only unchanged files from a remote repository and leave my local changes as they are, without any merges?

Someone suggested this sequence:

git stash
git pull
git stash pop

However, there's one significant drawback: there can be conflicts on the last command, and I need to do something to resolve them (like if I have tons of conflicting files this can get bothersome).

In TFS, I simply did "get latest version", no fuss no muss. Is there something similar here?

user3664916
  • 541
  • 6
  • 10
  • This question has particular conditions that keep it from being a duplicate of the suggested questions. – antlersoft May 22 '14 at 13:17
  • 1
    @antlersoft the question [git pull keeping local changes](http://stackoverflow.com/q/10414769/456814) is a suitable duplicate. –  May 23 '14 at 20:54

2 Answers2

6

TFS get latest version will also force you to do merges if their are conflicts with your changed files, so it's really no different than git pull.

It's generally easier to do a lot of small merges than one big merge at commit time, which is why git's workflow is the way it is.

It is possible (if not a good idea) to do what you ask in git; basically you are trying to defer all merges on files you've changed until you're ready to commit to origin. For git to track this properly, you will need to have a local branch separate from the branch you pull into.

git checkout -b mywork # Create local branch
# ... hack away ...

# When you want to get changes from origin.  Note
# that you can script this part to do it as one step.
git commit          # Commit my changes to local branch
git checkout master # Go back to branch tracking origin
git pull            # Retrieve changes from origin
git checkout mywork

# Here's the tricky part; merge from master, taking your
# code wherever there's a conflict, and *not* recording as
# a merge; if recorded as a merge, you'll overwrite changes
# in master when you merge back
git merge --squash --strategy-option=ours master

# ... hack away ...

When you are ready to push, you merge mywork into master and push from there.

Again, note that you are trading many smaller merges when you pull changes for one big merge when you merge mywork into master, and that's usually not what's best.

antlersoft
  • 14,636
  • 4
  • 35
  • 55
1

You can always just fetch the changes rather than pulling them.

When you fetch git pulls the changes from the server onto the remote branches, for example when you fetch master it would update origin/master. So you can review the new changes without affecting your local master branch.

Note When you use pull what git actually does is to fetch the changes and then merge them into your branches.

Sascha Wolf
  • 18,810
  • 4
  • 51
  • 73