• Register
0 votes
91 views

Problem :

I have the project with two branches as staging and beta.

I try to develop on staging, and use my master branch to fix the bugs.

git checkout master

And do the stuff on it :

git add fileToAdd
git commit -m "bug fixed"

And after that I merge with both branches:

git checkout staging
git merge master
git checkout beta
git merge beta

But now, when I am trying to change to my master branch, I'm getting below error:

error: Your local changes to the following files would be overwritten by checkout:
src/Pro/ConvocationBundle/Controller/DefaultController.php
Please, commit your changes or stash them before you can switch branches.
Aborting
6 5 3
7,540 points

2 Answers

0 votes

Solution :

Your error seems to appear when you have modified the file and the branch that you are switching to has changes for your file too.

Your options can be - commit, and after that amend your commit with extra changes ,you can modify your commits in git, as long as they are not pushed or - use the stash:

git stash save your-file-name
git checkout master
# do whatever you wanted to do with your master
git checkout staging
git stash pop

git stash save it will create stash that contains all your changes, but it will not be associated with any commit or even the branch.So git stash pop will apply latest stash entry to your current branch and it will restore saved changes and remove it from stash.

9 7 4
38,600 points
0 votes

Solution:

Your error seems at the time you have modified a file and the branch that you are switching to has changes for this file too (from advanced merge point).

Your options, as I view it, are - commit, and then amend this commit with extra changes (you can modify commits in git, as long as they're not impulse); or - use stash:

git stash save your-file-name
git checkout master
# do whatever you had to do with master
git checkout staging
git stash pop

git stash save will make stash that conceives your changes, however it isn't associated with any commit or even branch. git stash pop will employ advanced stash entry to your current branch, restoring saved changes and removing it from stash.

I encountered the similar problem and solved it by

git checkout -f branch

and its specification is more clear.

-f, --force

At the time switching branches, proceed even in case the index or the performing tree differs from HEAD. This is employed to throw away local changes.

At the time checking out paths from the index, do not fail upon unmerged entries; instead, unmerged entries are ignored.

You can force checkout your branch, in case you do not want to commit your local changes.

git checkout -f branch_name

I encountered the similar problem and solved it by

git checkout -f branch

Definitely, be careful with the -f switch. You will miss out any uncommitted changes in case you employ the -f switch. While there may be some use instances where it is useful to employ -f, in most instances, you may need to stash your changes and then switch branches. The stashing process is stated above.

Alright with the assistance of the other two answers I've come up with a direct solution:

git checkout HEAD^ file/to/overwrite
git pull

This performs for me to override all local changes and does not need an identity:

git reset --hard
git pull

Here is a solution that throws away staged changes:

git reset file/to/overwrite
git checkout file/to/overwrite

You can either commit your changes before you do the merge, or you stash them:

  1. git stash save

  2. git merge origin/master

  3. git stash pop

10 6 4
31,120 points

Related questions

0 votes
1 answer 116 views
116 views
Problem : How can I ignore the below error message on the Git pull? Your local changes to the following files would be overwritten by merge What if I just want to overwrite them? I Have already tried many things like git pull -f, but nothing really works for me.To be very clear, I just want to overwrite the specific changes and not the everything.
asked Jan 2 alecxe 7.5k points
0 votes
1 answer 123 views
123 views
Problem : I added the folder to the .gitignore file. Once I do the git status it tells me # On branch latest nothing to commit (working directory clean) But when I try to change my branches I get the below: My-MacBook-Pro:webapp marcamillion$ git checkout develop ... /system/images/* public/system/avatars/* How do I get this working for me so I can switch my branches without deleting those files?
asked Jan 22 jwilliam 3.9k points
0 votes
2 answers 2.1K views
2.1K views
Problem : On the branch I had some files in the .gitignore On the different branch those files are not. I am trying to merge the different branch into my branch, and I don't bother if those files are no longer overlooked or not. Unfortunately I get below ... by merge” How should I modify the pull command to overwrite those files, without needing to find, move or delete those files by myself?
asked Jan 6 alecxe 7.5k points
1 vote
2 answers 917 views
917 views
Problem : On the branch which I had some of my files in .gitignore On the different branch those files are not present. So I want to merge the different branch into my branch and I really do not care about those files if they are no longer ignored or not. But ... by merge How can I modify the pull command to overwrite the files, without me needing to find, move or delete the files by myself?
asked Dec 27, 2019 alecxe 7.5k points