Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+16 votes
4 views
in DevOps and Agile by (29.3k 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.

7 Answers

+13 votes
by (50.2k 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.

.gitignore and the following untracked working tree files would be overwritten by checkout
Intellipaat-community
by
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 (47.2k points)
Sometimes it happens if you do 'git checkout' without a branch name. To fix, do 'git checkout branchname'
by (41.4k points)
This answer was helpful.
+10 votes
by (19.7k points)

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

.gitignore and the following untracked working tree files would be overwritten by checkout
Intellipaat-community
by (29.3k points)
But I've heard that using -f option will delete the local files that are not indexed.
by (107k points)
I got this error: pathspec 'mybranch' did not match any file(s) known to git.
by (44.4k points)
This will delete all the local files which are not indexed, so be careful before doing this.
0 votes
by (107k 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 (29.5k points)

Try using the following :

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

0 votes
by (19.9k points)
edited by

Just force it : git checkout -f another-branch

0 votes
by (106k 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

0 votes
by (37.3k points)

Let’s understand the error first

Git is complaining about untracked files: When you try to switch branches, Git is warning you that some untracked files (which aren't added to the repository) will be overwritten if you switch branches. 

Solution without losing files:

  • Stash your untracked files temporarily:

git stash --include-untracked

This command will stash both tracked and untracked files, keeping them safe temporarily.

  • Switch branches

git checkout develop

  • Reapply the stashed files if needed:

git stash pop

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...