1

Precondition

In my git repo, I have two branch:

  • master
  • release-1.0

Master branch is front of release-1.0, and this repo using Golang program language and i use vendor dir, so vendor directory in master have many files not in release-1.0 branch.

Problem

The problem is when i checkout release-1.0 branch from master branch, i get many untracked files in working directory, such like this:

[root@Chine]# git status | head
# On branch release-1.0
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       vendor/github.com/GeertJohan/
#       vendor/github.com/PuerkitoBio/
#       vendor/github.com/appscode/
#       vendor/github.com/beorn7/
#       vendor/github.com/coreos/prometheus-operator/
#       vendor/github.com/daaku/

Is there any way to checkout with auto remove the untracked files?I have use git checkout -h to check some arguments, but not found anything useful.

My workaround way is:

# git rm -rf vendor
# git reset --hard origin/release-1.0

updated at: 2019-11-14

after compare some files, i find out the reason why this happen:

The problem is that in master branch i have add vendor directory to my git ignore list, and in release-1.0 it's not.

After following the comments below, i try git clean -fd, it works. But i still doesn't understand why there are untracked files exsits after checkout release-1.0.

Liqang Liu
  • 1,654
  • 3
  • 12
  • 20
  • 2
    Does this answer your question? [switch git branch without files checkout](https://stackoverflow.com/questions/1282639/switch-git-branch-without-files-checkout) – Viet Nov 04 '19 at 03:44
  • 3
    This kind of `git checkout` leaves *untracked* files behind. You can remove some or all untracked files with `git clean`. What's not clear to me is why you have untracked `vendor/` files, i.e., whether that's intentional ("we don't commit the vendored files") or accidental ("oops, we forgot to commit the vendored files"). – torek Nov 04 '19 at 05:21
  • I found out the reason is that in **master** branch i have add `vendor` directory to my git ignore list, and in **release-1.0** it's not. And `git clean -fd` works, so why the untracked file exists after check out **release-1.0**? – Liqang Liu Nov 14 '19 at 06:28

2 Answers2

1

I found out the reason is that in master branch i have add vendor directory to my git ignore list, and in release-1.0 it's not. And git clean -fd works, but don't known why the untracked file exists after check out release-1.0.

Liqang Liu
  • 1,654
  • 3
  • 12
  • 20
-1

You may use git checkout -f (or git checkout --force) Read about it from here

-f --force When switching branches, proceed even if the index or the working tree differs from HEAD. This is used to throw away local changes. When checking out paths from the index, do not fail upon unmerged entries; instead, unmerged entries are ignored.

Hrabovskyi Oleksandr
  • 3,070
  • 2
  • 17
  • 36
  • Untracked files aren't in the index so `git checkout --force` doesn't remove them. – phd Nov 04 '19 at 11:18