Manolo
Manolo

Reputation: 26350

Can't checkout master branch

I've been working in a branch with some code to improve an application. Now that it's finished, I want to merge this branch with master branch, but I can't. I first tried to checkout to master branch like this:

$ git checkout master

but I get this error:

error: The following untracked working tree files would be overwritten by checkout:
vendor/bin/doctrine
vendor/bin/doctrine.php
...

but the vendor directory is included in .gitignore file. If I execute:

$ git status

I get this message:

# On branch mejoras_contralador
nothing to commit (working directory clean)

After this, I would do:

$ git merge branch

How to avoid this error?

Upvotes: 0

Views: 8819

Answers (4)

Anshul Goyal
Anshul Goyal

Reputation: 76837

So the problem here is, that on your current branch (lets call it current), the vendor directory is in .gitignore and is not a part of the repo.

However, it is committed in your master branch and thus, your master branch is throwing the error The following untracked working tree files would be overwritten by checkout

The solution is simple.

1) In your branch current, rename your vendor directory to some other directory (say vendor.temp, you can use mv vendor vendor.temp)

2) Now do a git checkout master. This won't throw any conflicts this time, since there is no common vendor directory already existing on the branch you are trying to checkout from.

3) Now run a git rm -r --cached vendor on your master branch. This will remove the vendor directory from being tracked in your master branch.

4) Update the .gitignore file in your master branch to start ignoring vendor directory. Commit corresponding changes

5) Decide which vendor directory you need, the vendor directory in branch master, or vendor directory that was there in branch current, and rename/remove files accordingly.

Upvotes: 6

LazarusX
LazarusX

Reputation: 2735

At a first glance, the files should be ignored but have already been committed, and you need to remove them with git rm --cached.

However, according to your comment, it seems that the files are untracked in current branch but exist in the master branch? I think you need to delete the files in your in the current branch (move it somewhere else), check out the master branch, git rm --cached the files, and merge the two branches.

Upvotes: 0

wonde
wonde

Reputation: 1181

add the files(git add) and start tracking them and stash them (git stash) and then you can do git checkout master

Upvotes: 0

Andreas Gnyp
Andreas Gnyp

Reputation: 1840

Try the following:

git rm --cached vendor/bin/doctrine.php

and for all files you've ignored but get this status.

Upvotes: 0

Related Questions