• Register
0 votes
160 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

Please log in or register to answer this question.

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 134 views
134 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, 2020 alecxe 7.5k points
0 votes
1 answer 169 views
169 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, 2020 jwilliam 3.9k points
0 votes
2 answers 3K views
3K 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, 2020 alecxe 7.5k points
1 vote
2 answers 1.6K views
1.6K 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
0 votes
1 answer 1.7K views
1.7K views
Problem : I made some of the updates on my local machine, pushed them to the remote repository, and now I want to pull my changes to the server and I get a message; error: Your local changes to the following files would be overwritten by merge: wp-content/w3tc-config/master.php ... is the best), I just want to be able to merge a rest of my changes (plugin updates). Any clues how can I resolve it?
asked Jan 21, 2020 jwilliam 3.9k points
1 vote
2 answers 180 views
180 views
Problem : I need solution for bellow GIT error another git process seems to be running in this repository, e.g. an editor opened by 'git commit'.
asked Nov 8, 2019 peterlaw 6.9k points
0 votes
1 answer 33 views
33 views
Problem: I have basic knowledge of GIT. Whenever I try to pull in my project directory in my terminal then I face the below error: /Sites/branch1$ git pull origin master U app/config/app.php U app/config/database.php U app/routes.php Pull is not possible because you have ... 'git commit -a'. Why does my git is showing me above error? and how can a beginner like me fix above git pull error?
asked Aug 27, 2020 Raphael Pacheco 4.9k points
0 votes
2 answers 1.4K views
1.4K views
Problem : I am new to git. I am creating the new rails application but warning in git about LF replacement. I do git init git add . and then boom! I see this pop up for almost all files. I always face below warning usually if I build the application then it ... your working directory. warning: LF will be replaced by CRLF in README. I am unable to understand the message. How can I fix the message?
asked Feb 24, 2020 mphil 2.3k points