VS2017下使用Git遇到的問題


我在使用最新版的VS2017時,想獲取服務器上最新代碼,Fetch到了最新修改,但是在拉取代碼的時候出現了問題

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")

於是我采用常規套路(簽入,或者撤銷checkout) 都是失敗,而且不能undo change其中兩個文件storage.ide-shm/storage.ide-shm;

這個問題的最終結果就是導致我不能拉取最新代碼。

好在在StackOverflow發現如下方法

    git rm [path_and_filename] --cached  //該命令將永遠不想簽入的文件從索引(index)中移除,如果不想這個文件存在磁盤上,可以使用git rm [path_and_filename] --force
  1. 將操作后的文件(此時已經是staged)提交就好了

Ps:

  1. .gitignore 是最先想到的方法,但是也不行,不過SO上有人貌似是可以的,如下:
 # Visual Studio 2015 cache/options directory
 .vs/
  1. 微軟官檔:

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.

參考地址:

  1. .gitignore for C#: https://github.com/github/gitignore/blob/master/VisualStudio.gitignore#L28
  2. https://stackoverflow.com/questions/45016619/what-is-the-storage-ide-file-beneath-my-visual-studio-solution-folder-and-wha
  3. https://stackoverflow.com/questions/45802083/visual-studio-2017-15-3-0-git-changes-include-storage-ide-even-though-vs-in
  4. https://docs.microsoft.com/en-us/vsts/git/tutorial/ignore-files?tabs=visual-studio

Ps2: 加一個描述更詳細的說明

參考地址:http://www.pfeng.org/archives/840


1. 關於將已經ignore的文件(夾)重新加入跟蹤里面

重新添加已經被忽略過的文件(夾)

重新添加已經被忽略過的文件時,我們僅僅使用git add是不行的,因為git倉庫中根本沒有那個文件,這時候我們需要加上-f參數來強制添加到倉庫中,然后在提交。比如上面設置了忽略排除的文件TokenGuard.php我們需要重新加入

git add -f [文件(夾)名]

然后在commit和push就行了


2. 關於VS中經常出現的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服務器上的代碼覆蓋;我當然不想剛剛寫的代碼被覆蓋掉,看了git的手冊,發現可以這樣解決:

方法1:如果你想保留剛才本地修改的代碼,並把git服務器上的代碼pull到本地(本地剛才修改的代碼將會被暫時封存起來)

 git stash  
 git pull origin master  
 git stash pop  

如此一來,服務器上的代碼更新到了本地,而且你本地修改的代碼也沒有被覆蓋,之后使用add,commit,push 命令即可更新本地代碼到服務器了。

方法2、如果你想完全地覆蓋本地的代碼,只保留服務器端代碼,則直接回退到上一個版本,再進行pull:

 git reset --hard  
 git pull origin master  

注:其中origin master表示git的主分支。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM