+18 votes
1 view
in Devops and Agile by (25.5k points)
edited by

So I added a folder to my .gitignore file.

Once I do a git status it tells me

# On branch latest

nothing to commit (working directory clean)

However, when I try to change branches I get the following:

My-MacBook-Pro:webapp marcamillion$ git checkout develop

error: The following untracked working tree files would be overwritten by checkout:

.......

Please move or remove them before you can switch branches.

Aborting

This is what my .gitignore file looks like:

.bundle

.DS_Store

db/*.sqlite3

log/*.log

tmp/**/*

public/system/images/*

public/system/avatars/*

How do I get this working so I can switch branches without deleting those files?

If I make a change, will it affect those files? In other words, if I came back to this branch afterward would everything be perfect as up to my latest commit?

I don't want to lose those files, I just don't want them tracked.

6 Answers

+15 votes
by (42.7k points)

The question seems like you want the files ignored but they have already been committed. 

For that .gitignore does not affect files that are already in the repo so they need to be removed with

 git rm --cached

 The --cached will prevent it from having any effect on your working copy. After the files are removed from the repo then the .gitignore will prevent them from being added again.

But the other problem with your .gitignore, you are excessively using wildcards and it's causing it to match less than you expect it to.

For that see this .gitignore file

.bundle

.DS_Store

db/*.sqlite3

log/*.log

tmp/

public/system/images/

public/system/avatars/

This will help you to overcome this issue.

For more commands, you can visit git commands or you can read the git tutorial prepared by the experts.

by (25.3k points)
Thanks. By removing all the files from the current branch and backing them up. Then switching branches and put them back. That worked. Also, thanks for the tip on the .gitignore though
by (33.1k points)
Thanks for this clear explanation!
by (43.4k points)
Sometimes it happens if you do 'git checkout' without a branch name. To fix, do 'git checkout branchname'
by (32.5k points)
This answer was helpful.
+12 votes
by (25k points)

Just force it to checkout to another branch: git checkout -f another-branch

by (25.5k points)
But I've heard that using -f option will delete the local files that are not indexed.
by (63.9k points)
I got this error: pathspec 'mybranch' did not match any file(s) known to git.
by (43.5k points)
This will delete all the local files which are not indexed, so be careful before doing this.
0 votes
by (63.9k points)

If you're on OS X, it may be because a file's name has had some characters change case. Try setting the following config option:

git config core.ignorecase true

0 votes
by (29k points)

Try using the following :

 1. git fetch --all
 2. git reset --hard origin/{branch_name}

0 votes
by (12.4k points)
edited by

Just force it : git checkout -f another-branch

0 votes
by (107k points)

Almost all of the answers consider deleting or removing the files, which is the easy way. But sometimes you do not want to delete or remove your local files. So, what you can do is merge them with a strategy, so git has a solution for this too. Use the below mentioned code:-

git merge --strategy=ours master

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...