0

I am new to git so please bear with me. I have a rails application on my local machine that I am experimenting with and pushing to the master branch periodically. It works at the moment, but I have fallen behind, and now I am many commits behind the master.

$ git branch
* master


$ git status
On branch master
Your branch is behind 'origin/master' by 27 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)
nothing to commit, working tree clean

However, when I use git pull and then start rails, my application breaks with a precompiler error. So I am forced to use git --reset to go back to the local commit before I used git pull.

What is the right way to get around this issue and merge with the latest changes on the master branch? Would one use git --rebase in this case?

the_darkside
  • 5,688
  • 7
  • 36
  • 83
  • I think one of those 27 commits broke the app. Where did those come from? – Alejandro C. Sep 11 '17 at 19:14
  • `git pull -r` will pull those changes on top of your local. In terms of the precompile, can you post the error? Usually those are related to syntax errors in your assets.You can take a look at https://stackoverflow.com/a/8259998/2048680 – Conor Sep 11 '17 at 19:15

2 Answers2

1

Try

git stash
git pull origin master

And once it updates, git stash apply to reapply your local changes

veggirice
  • 152
  • 1
  • 12
  • When I try this, then I get 5 merge conflicts – the_darkside Sep 11 '17 at 19:24
  • you need resolve the conflicts by choosing which version of the code/file you would like to keep. – veggirice Sep 11 '17 at 19:26
  • there's also this: https://stackoverflow.com/questions/161813/how-to-resolve-merge-conflicts-in-git – veggirice Sep 11 '17 at 19:28
  • 1
    Whoa, something's fishy here. In the question OP showed a `git status` that said the working tree is clean and that there's nothing to commit - so what exactly would stashing do, that would end up leading to merge conflicts? – Mark Adelsberger Sep 11 '17 at 19:43
  • `git stash` is just a practice i follow to put away my local changes first, then pull the latest from the branch. after that, I would reapply my uncommitted changes (resolve any conflicts) before making the changes ready for a push to the same branch – veggirice Sep 11 '17 at 19:47
0

Since nobody has stated this clearly yet: You ask

What is the right way to get around this issue and merge with the latest changes on the master branch?

When you do git pull that does merge the remote changes into your current branch. Whether you would choose to do a rebase instead of a merge (per your other question) is a separate issue, but the default behavior is to combine the two sets of changes (local and remote).

More precisely, by default git pull does a fetch followed by a merge. The exact merge operation depends on configuration and on command-line options, but in a typical configuration where origin/master is upstream of master, saying

git pull

will merge origin/master into master.

So why the errors?

One possibility is that there were merge conflicts. If that happens, git will tell you. If you say git status in this condition, it will tell you that there's a merge in progress and it will indicate which paths (files) need conflict resolution.

Another possibility is that the changes don't conflict (in that they don't affect the same region of the same file) but still don't work properly together. That you would simply have to debug.

Mark Adelsberger
  • 32,904
  • 2
  • 24
  • 41