VS2017下使用Git遇到的問題
我在使用最新版的VS2017時,想獲取服務(wù)器上最新代碼,F(xiàn)etch到了最新修改,但是在拉取代碼的時候出現(xiàn)了問題
user@user-PC MINGW64 /d/demo/myrepos (dev-css)
$ git pull origin dev-css
From https://winleechina.visualstudio.com/_git/PartTimeJob
* branch dev-css -> FETCH_HEAD
error: Your local changes to the following files would be overwritten by merge:
BaseSite/Code/.vs/BaseSite/v15/Server/sqlite3/storage.ide-shm
BaseSite/Code/.vs/BaseSite/v15/Server/sqlite3/storage.ide-wal
BaseSite/Code/.vs/BaseSite/v15/sqlite3/storage.ide-shm
BaseSite/Code/.vs/BaseSite/v15/sqlite3/storage.ide-wal
Please commit your changes or stash them before you merge.
Aborting
Updating d568290..cf89950
user@user-PC MINGW64 /d/demo/myrepos (dev-css)
$ git status
On branch dev-css
Your branch is behind 'origin/dev-css' by 8 commits, and can be fast-forwarded.
(use "git pull" to update your local branch)
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: BaseSite/Code/.vs/BaseSite/v15/Server/sqlite3/storage.ide-shm
modified: BaseSite/Code/.vs/BaseSite/v15/sqlite3/storage.ide-shm
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitattributes
.gitignore
no changes added to commit (use "git add" and/or "git commit -a")
于是我采用常規(guī)套路(簽入,或者撤銷checkout) 都是失敗,而且不能undo change其中兩個文件storage.ide-shm/storage.ide-shm;
這個問題的最終結(jié)果就是導(dǎo)致我不能拉取最新代碼。
好在在StackOverflow發(fā)現(xiàn)如下方法
git rm [path_and_filename] --cached //該命令將永遠(yuǎn)不想簽入的文件從索引(index)中移除,如果不想這個文件存在磁盤上,可以使用git rm [path_and_filename] --force
- 將操作后的文件(此時已經(jīng)是staged)提交就好了
Ps:
- .gitignore 是最先想到的方法,但是也不行,不過SO上有人貌似是可以的,如下:
# Visual Studio 2015 cache/options directory
.vs/
- 微軟官檔:
Permanently ignore changes to a file
If a file is already tracked by Git, adding that file to your .gitignore is not enough to ignore changes to the file. You also need to remove the information about the file from Git's index
These steps will not delete the file from your system. They just tell Git to ignore future updates to the file.+
1. Add the file in your .gitignore.
2. Run the following:
> git rm --cached <file>
3. Commit the removal of the file and the updated .gitignore to your repo.
參考地址:
- .gitignore for C#: https://github.com/github/gitignore/blob/master/VisualStudio.gitignore#L28
- https://stackoverflow.com/questions/45016619/what-is-the-storage-ide-file-beneath-my-visual-studio-solution-folder-and-wha
- https://stackoverflow.com/questions/45802083/visual-studio-2017-15-3-0-git-changes-include-storage-ide-even-though-vs-in
- https://docs.microsoft.com/en-us/vsts/git/tutorial/ignore-files?tabs=visual-studio
Ps2: 加一個描述更詳細(xì)的說明

參考地址:http://www.pfeng.org/archives/840
1. 關(guān)于將已經(jīng)ignore的文件(夾)重新加入跟蹤里面
重新添加已經(jīng)被忽略過的文件(夾)
重新添加已經(jīng)被忽略過的文件時,我們僅僅使用git add是不行的,因為git倉庫中根本沒有那個文件,這時候我們需要加上-f參數(shù)來強制添加到倉庫中,然后在提交。比如上面設(shè)置了忽略排除的文件TokenGuard.php我們需要重新加入
git add -f [文件(夾)名]
然后在commit和push就行了
2. 關(guān)于VS中經(jīng)常出現(xiàn)的error: Your local changes to the following files would be overwritten by merge:
error: Your local changes to the following files would be overwritten by merge:
意思是我本地上新修改的代碼的文件,將會被git服務(wù)器上的代碼覆蓋;我當(dāng)然不想剛剛寫的代碼被覆蓋掉,看了git的手冊,發(fā)現(xiàn)可以這樣解決:
方法1:如果你想保留剛才本地修改的代碼,并把git服務(wù)器上的代碼pull到本地(本地剛才修改的代碼將會被暫時封存起來)
git stash
git pull origin master
git stash pop
如此一來,服務(wù)器上的代碼更新到了本地,而且你本地修改的代碼也沒有被覆蓋,之后使用add,commit,push 命令即可更新本地代碼到服務(wù)器了。
方法2、如果你想完全地覆蓋本地的代碼,只保留服務(wù)器端代碼,則直接回退到上一個版本,再進行pull:
git reset --hard
git pull origin master
注:其中origin master表示git的主分支。


浙公網(wǎng)安備 33010602011771號