使用VSCode的源代码控制选项卡查看git提交之间的更改。

huangapple go评论91阅读模式
英文:

Use VSCode Source Control tab to view changes between git commits

问题

在我正在使用的VSCode版本(1.65)中,源代码控制选项卡似乎只关注当前未提交的更改。如果我编辑一个文件,它会显示在源代码控制下拉菜单中,并显示所做更改的漂亮图形。我想使用这个图形来显示前一次提交和当前提交之间的差异,而不是当前提交和未提交的项目。基本上,我想要在漂亮的VSCode源代码控制图形界面中显示所有来自"git show HEAD"的信息。我不能安装更多的程序,所以"使用git图形用户界面客户端"对我来说不是一个选择。

我知道已经存在一个名为"Timeline"的功能,但这个功能是基于文件的。我不知道在前一次提交中更改了哪些文件,所以对我来说,时间线功能是无用的,除非我查看我的存储库中的所有内容。

如果这很容易做到或者已经是一个我没有看到的现有功能,请停止阅读并给出您的回应。下面是我尝试的解决方法,是一个不太理想的解决办法。

我尝试解决这个问题的方法是尝试"取消提交"最近的更改,这样我的存储库会认为它处于HEAD-1状态,但HEAD中的所有更改都在那里并且未提交。我找不到一个简单的命令来做到这一点,所以我在Linux中从存储库的基本目录开始执行了以下操作:

cd ..
mkdir temp_repo
cp -r ./repo/. ./temp_repo/     # 将所有内容移动到临时文件夹
cd temp_repo
rm -rf .git                     # 从临时文件夹中删除git信息
cd ../repo
git checkout previous_version   # 切换到上一个版本
find . ! -name "*.git" -delete  # 删除除git信息以外的所有内容
cp -r ../temp_repo/. .

基本上,我将当前版本的存储库复制到一个临时文件夹中,从该临时文件夹中删除了git信息,然后将存储库切换到旧版本。在旧版本中,我尝试删除除了git信息以外的所有内容,然后将临时文件夹的内容复制回去。我希望在前一版本的git信息的基础上,但是使用当前版本的文件,git会将新版本中的所有更改识别为未提交的更改。

这种方法几乎可以工作。如果我跳过倒数第二个命令,不删除前一版本存储库中的所有非git项目,git会按照我想要的方式工作,并且VSCode源代码控制会显示所有更改。唯一的问题是,如果其中一个更改是删除文件,这种方法将无法显示,而我需要它。这就是为什么我尝试在复制新版本之前删除除git信息以外的所有内容,但是它没有起作用,即使.git文件在其中,git也停止将/repo/识别为git存储库。

如果有更好的方法可以"取消提交"最近的更改,就像我描述的那样,我会很乐意听到。如果没有,是否有办法使我的解决方案生效?也就是说,我可以使用什么命令来代替find . ! -name "*.git" -delete,以删除所有源文件同时保留git信息?

英文:

In the version of VSCode I am using (1.65), The source control tab seems to be focused only on current uncommitted changes. If I edit a file, it shows up under the source control dropdown and shows a nice graphic of the changes made. I would like to use this graphic to show the differences between the previous commit and the current commit instead of the current commit and uncommitted items. Basically, I want all of the information from "git show HEAD" to be displayed in the nice VSCode source control graphic interface. I cannot install more programs, so "use a git gui client" isn't an option for me.

I know that there is a "Timeline" feature that already exists, but this works on a file-by-file basis. I do not know which files were changed in the previous commit, so the timeline feature is useless to me unless I comb through everything in my repository.

If this is easy to do or already an existing feature that I didn't see, please stop reading and give your response. My attempted solution below is a gross workaround.

How I tried to solve this problem was by trying to "decommit" the most recent changes, so that my repo thinks it's at HEAD-1 but all of the changes made in HEAD are there and uncommitted. I could not find a simple command to do this, so instead I did the following in linux starting from the base directory of the repository:

cd ..
mkdir temp_repo
cp -r ./repo/. ./temp_repo/     #move everything to temporary folder
cd temp_repo
rm -rf .git                     #delete git information from temp folder
cd ../repo
git checkout previous_version   #switch to previous version
find . ! -name "*.git" -delete  #delete everything except git info
cp -r ../temp_repo/. .

Basically, I copied the current version of the repo into a temporary folder, removed the git information from that temporary folder, then checked out the repo to the old version. In the old version, I tried to delete everything except for the git information, and then copy the contents of the temporary folder back in. I hoped that with the git information from the previous version but the files of the current version, git would recognize everything that changed in the new version as uncommitted changes.

This approach almost works. If I skip the second to last command deleting every non-git item from the previous version of the repo, git does what I want and VSCode source control shows all of the changes as changes. The only problem is that if one of the changes was deleting a file, this method will not show that, and I need it to. That's why I tried deleting everything except git information before copying the new version in, but it didn't work and git stopped recognizing /repo/ as a git repository despite the .git file being in there.

If there is a better way to "decommit" the most recent changes like I described I'd love to hear it. If not, is there a way to make my solution work? I.e. what command could I use instead of find . ! -name "*.git" -delete that would remove all of the source files while preserving git information?

答案1

得分: 0

这是翻译后的内容:

这不是使用源代码控制选项卡,但这是一个非常接近所需功能的解决方案。

首先,编辑您的 git 设置,使 VSCode 成为 git difftool 命令的默认工具。运行:

git config --global -e

以编辑 git 配置,并粘贴以下设置:

[core]
  editor = code --wait
[diff]
  tool = vscode
[difftool "vscode"]
  cmd = code --wait --diff $LOCAL $REMOTE
[merge]
  tool = vscode
[mergetool "vscode"]
  cmd = code --wait $MERGED

现在,在 VSCode 中找到您希望比较的两个提交 ID,您可以使用 git log 找到最近的提交。

运行以下命令:(如果提交 1 是两者中较旧的提交,则最有意义)

git difftool --dir-dif (提交 ID 1) (提交 ID 2)

您将获得一个包含两个文件夹的 VSCode 浏览器窗口,每个提交一个文件夹。这些文件夹只包含已更改的文件。

要获取文件之间的单独更改,请首先右键单击顶部名为 "left" 的文件夹中的要比较的文件,然后选择 "select for compare" 选项。然后,右键单击 "right" 文件夹中相同的文件,并选择 "compare with selected"。您将获得一个窗口,显示两个提交之间所做的所有更改。

英文:

It is not using the source control tab, but this is a solution that gets very close to the desired functionality.

First, edit your git settings to make VSCode the default tool for the git difftool command. Run:

git config --global -e

to edit the git config, and paste these settings in:

[core]
  editor = code --wait
[diff]
  tool = vscode
[difftool "vscode"]
  cmd = code --wait --diff $LOCAL $REMOTE
[merge]
  tool = vscode
[mergetool "vscode"]
  cmd = code --wait $MERGED

Now, find the two commit IDs you wish to compare in VSCode, you can find the most recent commits with git log

Run the command: (It makes the most in sense if commit 1 is the older of the two.)

git difftool --dir-dif (commit ID 1) (commit ID 2)

You will get a VSCode explorer window with two folders, one for each commit. These folders will only contain files that have changed.

To get individual changes between files, first right click the file you want in the top folder called "left" and choose the "select for compare" option. Then, right click the same file in the "right" folder and choose "compare with selected". You will get a window that shows all the changes that were made between the two commits.

huangapple
  • 本文由 发表于 2023年6月15日 07:45:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478244.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定