gitignore and “The following untracked working tre

2019-01-02 16:05发布

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:
    public/system/images/9/thumb/red-stripe.jpg
    public/system/images/9/original/red-stripe.jpg
    public/system/images/8/thumb/red-stripe-red.jpg
    public/system/images/8/original/red-stripe-red.jpg
    public/system/images/8/original/00-louis_c.k.-chewed_up-cover-2008.jpg
    public/system/images/7/thumb/red-stripe-dark.jpg
    public/system/images/7/original/red-stripe-dark.jpg
    public/system/images/7/original/DSC07833.JPG
    public/system/images/6/thumb/red-stripe-bw.jpg
    public/system/images/6/original/website-logo.png
    public/system/images/6/original/red-stripe-bw.jpg
    public/system/images/5/thumb/Guy_Waving_Jamaican_Flag.jpg
    public/system/images/5/original/logocompv-colored-squares-100px.png
    public/system/images/5/original/Guy_Waving_Jamaican_Flag.jpg
    public/system/images/4/thumb/DSC_0001.JPG
    public/system/images/4/original/logo.png
    public/system/images/4/original/DSC_0001.JPG
    public/system/images/4/original/2-up.jpg
    public/system/images/3/thumb/logo2.gif
    public/system/images/3/original/logo2.gif
    public/system/images/3/original/Guy_Waving_Jamaican_Flag.jpg
    public/system/images/3/original/11002000962.jpg
    public/system/images/2/thumb/Profile Pic.jpg
    public/system/images/2/original/Profile Pic.jpg
    public/system/images/2/original/02 Login Screen.jpg
    public/system/images/1/original/Argentina-2010-World-Cup.jpg
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 afterwards 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.

27条回答
与君花间醉酒
2楼-- · 2019-01-02 16:45

For those who need something less far-reaching than Scott Schafer’s answer,

git clean -f

will likely work. I highly suggest running

git clean --dry-run

first. That command will output a list of files that Git will remove if you run git clean -f, and might save you the pain of inadvertently removing something you didn’t want to.

See this Stack Oveflow answer or the docs for more information on git clean.

查看更多
像晚风撩人
3楼-- · 2019-01-02 16:45

Delete .gitignore file from appname/gen/ to solve this issue.

查看更多
看风景的人
4楼-- · 2019-01-02 16:48

This worked for me.

 1. git fetch --all
 2. git reset --hard origin/{branch_name}
查看更多
谁念西风独自凉
5楼-- · 2019-01-02 16:50

Most of the answers consider deleting or removing the files, which is the easy way. But sometimes you don't want to get rid of the local files. But merge with a strategy, so git has solution for this too ;

git merge --strategy=ours master 
查看更多
梦寄多情
6楼-- · 2019-01-02 16:52

Move files, instead of delete

One way of avoiding deleting files is to move them instead. For example:

cd "`git rev-parse --show-toplevel`"
git checkout 2>&1 | while read f; do [ ! -e "$f" ] || mv "$f" "$f".bak; done
查看更多
无色无味的生活
7楼-- · 2019-01-02 16:53

This happened to me on a Windows 8 system, using Git from the command prompt. The rest of my team uses TFS, and I use Microsoft's git-tf to push/pull between TFS and my local Git repository.

The problem arose due to some files that had been renamed only to change their case. What appears to have happened was this:

  • The files were checked in with mixed casing in their names.
  • In a later commit, the file names were changed to all lower-case.
  • git-tf initially got the files in mixed case.
  • When the files were renamed to lower-case, git-tf didn't get the files because to Windows 8 those file names are equivalent.
  • Since Git is case-sensitive, it complained that I had the mixed-case files that weren't in source control. But using git status, I couldn't see any changes, since in the Windows command prompt those file names are equivalent.

The simplest solution for me was:

  • git checkout a previous version of the project, well before those files were ever added.
  • Then git checkout the latest version of the project, with the correct file casing.
查看更多
登录 后发表回答