5

I have two servers for development and production. In the development server I have a new branch merged with origin. Now I want to go to production and pull the changes. But in production server in branch master I have modified two files which is needed only for this server.

So when I hit:

git pull

I get:

error: Your local changes to the following files would be overwritten by merge:
app/code/local/NextBits/DependentCustomOptionsImage/Block/Adminhtml/Template/Edit/Tab/Options/Option.php
app/design/adminhtml/default/default/layout/dependentcustomoptionsimage.xml
Please, commit your changes or stash them before you can merge

These are the two files I need.

How can I pull the origin and without overwriting these files?

Or how can I keep these files, pull the changes and revert them back?

phd
  • 82,685
  • 13
  • 120
  • 165
G. G.
  • 279
  • 1
  • 4
  • 14
  • 1
    Possible duplicate of [How do I resolve git saying "Commit your changes or stash them before you can merge"?](https://stackoverflow.com/questions/15745045/how-do-i-resolve-git-saying-commit-your-changes-or-stash-them-before-you-can-me) – phd Nov 21 '18 at 12:31
  • https://stackoverflow.com/search?q=%5Bgit%5D+error%3A+Your+local+changes+to+the+following+files+would+be+overwritten+by+merge – phd Nov 21 '18 at 12:31
  • @phd this question you mentioned didn't help me. In fact I lost some files. The git I have is for a magento site and not all files are in origin – G. G. Nov 21 '18 at 12:37

1 Answers1

6

There are at least three different solutions to achieve this:

  • You can commit your changes on these two files in the production server, before running git pull:

    git add -v -u
    git commit -m "Save prod config"
    git pull
    

    This will typically create a merge commit combining the local branch (with the commit "Save prod config") and the changes from your remote.

    If there is some conflict, you'll just need to resolve it with the help of git status, git diff, and git commit.

    For subsequent updates, it should suffice to run git pull.

  • Like the previous solution, you can commit your changes on the two production config files, but run git pull -r afterwards:

    git add -v -u
    git commit -m "Save prod config"
    git pull -r
    

    The -r option stands for --rebase and means that the local commit(s) specific to the production server will be rebased on top of the pulled commits.

    Subsequent updates can be performed by running git pull -r.

    The main advantage of this solution is just the ability to keep a "linear history": there is no merge commit created and the "Save prod config" commit will always be replayed at the 'end' of the history.

  • or you can use the git stash command (which may have some drawbacks in case of conflict):

    git stash
    git pull
    git stash pop
    
ErikMD
  • 13,377
  • 3
  • 35
  • 71
  • after `git commit -m "Save prod config"` I get: `Your branch is behind 'origin/master' by 3 commits, and can be fast-forwarded`. Why this is happening? – G. G. Nov 21 '18 at 12:32
  • @G.G. in general the message "your branch is behind... and can be fast-forwarded" is not a warning, but just an indication that "git pull" wouldn't create a merge commit because master happens to be a direct ancestor of origin/master, so "git pull" would just update the local branch by moving the local branch to the latest upstream commit. However, it seems to me this situation should not have occurred given the use case you described in your question. Maybe check you have indeed run "git add ..." and that the commit has been successfully created in the good branch. – ErikMD Nov 21 '18 at 13:12
  • Instead of `git merge`, you could also use `git fetch` and a `git merge --no-ff --no-commit` if you want to review changes to production before you change something... – Christoph Nov 21 '18 at 15:11
  • @ErikMD understood. But the only change I have made in this server are the files I already mentioned. Please if I fast forward the master branch will I loose anything? – G. G. Nov 21 '18 at 19:34
  • @G.G. a fast-forward merge can't cause losing files. In general, with Git you can't lose changes, provided they have been committed at some point. E.g., you can always rollback `HEAD` to some previous state after inspecting its history with `git reflog`. But you said in [a previous comment](https://stackoverflow.com/questions/53411493/git-stash-uncommitted-files/53411658?noredirect=1#comment93699035_53411493) you had lost changes (maybe with a `git checkout -- filenames` command? which is indeed a destructive one) so hopefully you've already saved (committed) the files mentioned in your post. – ErikMD Nov 21 '18 at 20:17
  • @ErikMD I lost the files with git checkout command, you are right . I’ll keep the two files and make a fast forward to check. – G. G. Nov 21 '18 at 20:27