VS2017下使用Git遇到的问题

时间:2022-03-30 23:51:16

我在使用最新版的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;

这个问题的最终结果就是导致我不能拉取最新代码。

好在在*发现如下方法

  1. VS2017下使用Git遇到的问题
    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://*.com/questions/45016619/what-is-the-storage-ide-file-beneath-my-visual-studio-solution-folder-and-wha
  3. https://*.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: 加一个描述更详细的说明

VS2017下使用Git遇到的问题

参考地址: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的主分支。