使用 git commit 和 git stash 之外的替代方法来切换分支时应该使用什么?

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

What to use instead of git commit and git stash when changing branches?

问题

我想分享我的不便之处,看看你的解决方案或意见。

我正在使用git、VSCode和BitBucket。

假设我正在处理一个庞大的代码库,位于分支FEAT-some-new-feature上。这个功能需要对很多文件进行更改,所以git的变更跟踪在浏览代码库时非常有帮助。

我需要在另一个分支FIX-some-fix上进行一些工作。如果我不提交我的工作,git不会允许我切换分支。如果我提交我的工作,那么跟踪就会丢失,这是一个很大的不便。

我可以使用git stashgit stash pop,但一旦我将更改存储起来,就没有直接指示分支已经存储了更改。如果我在两周后回到FEAT-some-new-feature,我可能会忘记我在那里存储了更改。这可能会引起恐慌,如果我特别愚蠢的话,我可能会开始从头开始重写整个功能。

你提出了什么解决方案?如何在不丢失更改跟踪或忘记已经存储更改的情况下切换分支?也许有我不知道的git功能,可以提交更改而不会丢失已跟踪的文件更改?

英文:

I'd like to share my inconveniences and see your solutions or opinions.

I'm using git, VSCode and BitBucket.

Let's say I'm working on a big codebase and on a branch FEAT-some-new-feature. A lot of files need changes for this feature so the git changes tracking helps out a ton when navigating the codebase.

I need to do some work on this other branch FIX-some-fix. Git won't let me change branches unless I commit my work. If I commit my work, the tracking will be lost, which is a big inconvenience.

I can use git stash and git stash pop, but once I stash the changes, there is no direct indication that the branch has stashed changes. If I go back to FEAT-some-new-feature after two weeks, I might forget that I had changes stashed there. That can cause panic and if I'm being particularly stupid, I might start rewritting the whole feature from scratch.

What solution do you propose? How can I change branches without losing my change tracking or forgetting I have changes stashed? Maybe there is a git feature I don't know where you can commit changes without losing tracked file changes?

答案1

得分: 1

"stash"仍然是每种情况下的最佳解决方案,但确实,人们往往会在之后忘记它。因此,您可以尝试至少两种方法:

重置常规提交

如果您需要在几天内切换到其他工作,您可以将当前工作目录的完整状态添加并提交为一个名为backup或其他名称的单个提交,然后切换到其他分支。

当您想要返回到以前的任务并恢复工作时,首先切换到分支(这将使您回到备份提交的“干净”状态),然后使用git resetgit reset --mixedmixed是默认选项)重置分支到以前的提交。

这将将分支和索引还原到以前的提交,同时保留工作目录的内容,并且您将恢复git status曾经显示的跟踪状态。

请注意,这涉及重写历史。如果您需要将其推送到本地机器之外,请首先确保服务器允许您这样做。

使用git worktree

git worktree将使您能够在常规工作目录旁边打开一个临时的工作目录。当您需要检出同一存储库的两个不同版本时,或者需要比较库的不同版本时,这特别有用。

在您的特定情况下,这是一种方便的方法,可以在不修改您的常规工作地点的情况下处理项目的另一部分。

这样,您就不需要重写历史,但人们往往会忘记这些额外的目录,因此请确保在完成时完全清理它。

英文:

stash remains the best solution in every case but, indeed, one tends to forget about it afterwards. So there's at least two approaches you may try:

Resetting a regular commit

If you need to switch onto something else for a couple of days or something, you may just add the complete state of your current working directory and commit it into a single commit named backup or something, then checking out your other branch.

When you want to go back to your former task and resume your work, first check out the branch (which will bring you back to your backup commit in a "clean" state), then reset the branch to the previous commit using git reset or git reset --mixed (mixed being the default option).

This will bring back the branch and the index to the previous commit while preserving the working directory's content, and you'll retrieve the tracking status that git status used to show.

Be aware though that it's about rewritting history. If you need to push that beyond your local machine, ensure first that the server will let you do.

Use git worktree

git worktree will enable you to open a secondary, temporary working directory aside of the regular one. It's especially useful when you need to checkout two distinct versions of a same repository, when you need compare versions of a library, for instance.

In your particular case, it's a convenient way to work on another part of a project without the need to alter you regular work place.

This way, you won't need to rewrite history, but one tends to forget about those additional directories too, so make sure to clean it up completely when you're done with it.

答案2

得分: 1

我相信储藏(stashes)实际上是解决您问题的方法。

> [...] 两周后,我可能会忘记我曾经将更改储藏在那里。

您不必忘记储藏。当您运行git log --oneline --all --graph命令来查看整个树时,通常不包括“储藏版本”,唯一的例外是您最近的储藏。

要使所有储藏可见,请在每次运行该命令时包括--reflog。reflog是通常不会出现的对象的历史记录,大多数情况下是因为它们被Git视为垃圾,或者它们是储藏。

英文:

I believe that stashes are actually the solution to your problem.

> [...] after two weeks, I might forget that I had changes stashed there.

You don't have to forget about stashes. When you run your git log --oneline --all --graph command to see the whole tree, it normally doesn't include "stashed versions," with the only exception being your most recent stash.

To make all stashes visible, include --reflog in the aforementioned command every time you run it. The reflog is history of objects that would normally not appear, in most cases because they are considered garbage by Git or they are stashes.

答案3

得分: 0

看起来你的工作目录中有许多未提交的更改已经存在很长时间了。这不是使用Git(或任何版本控制系统)的推荐方式。你应该经常进行小而逻辑清晰的提交。也许你的任务很大,应该将其分解成较小的任务。

另一方面,如果你还没有使用远程特性分支的解决方案,也许可以帮助你。将你的更改提交到你正在工作的分支,并将其推送到origin。创建一个草稿拉取请求(不确定Bitbucket是否已经支持PR草稿),然后你仍然可以在将它们合并到main分支之前审查你的整体更改。

为什么要经常提交?冲突更容易处理,且丢失工作的可能性更小(当你经常推送时,这个可能性甚至更小)。

英文:

It seems you have many uncommited changes in your working dir for a long time. That is not a recommended way to use git (or any VCS). You should make small, logical commits often. Maybe your task is really big and should be divided in smaller tasks.

On the other hand, maybe a sollution with remote feature branch could help if you are note using it. Commit your changes to the branch you are working on and push it to origin. Create a draft pull request (not sure if Bit bucket supports PR drafts already), then you can still review your overall changes, before merging them into main branch.

Why you should commit often? Conflicts are easier to handle and the possibility of loosing the work is smaller (when you push often its even smaller)

huangapple
  • 本文由 发表于 2023年7月10日 22:21:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76654678.html
匿名

发表评论

匿名网友

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

确定