Git checkout 不会改变任何东西

发布于 2024-10-09 01:15:07 字数 1226 浏览 5 评论 0原文

我真的很喜欢吉特。至少,我喜欢 git 的想法。能够将我的主项目作为一个单独的分支进行检查,在那里我可以更改我想要的任何内容,而不用担心搞砸其他所有事情,这真是太棒了。但这不起作用。

基本上,我的工作流程是这样的:

  1. 将稳定版本签出到新分支以试验新代码
  2. 进行一系列更改 - 我无意保留任何这些,我只是在试验。
  3. 查看我更改的所有内容
  4. 添加要跟踪的所有更改
  5. 提交分支并将分支推送到原点(如果有效,否则跳过此步骤)
  6. 决定尝试另一种方法,返回 master
  7. 观察实验中的工件 文件

每当我将一个分支签出到另一个分支,对一个分支进行更改,然后签出原始分支时,我仍然拥有所有 以及另一个分支中发生的变化。这变得非常令人沮丧。我读到,当您在执行此操作时在 IDE 中打开文件时,可能会发生这种情况,但我对此非常小心,并且在切换之前都关闭了 IDE 中的文件,关闭了 IDE,并关闭了我的 Rails 服务器分支,这种情况仍然会发生。此外,运行“git clean -f”要么删除任意提交后发生的所有内容(并且是随机的),要么像最新的情况一样,不将任何内容更改回其原始状态。

我以为我正确使用了 git,但此时我已经无计可施了。我正在尝试使用项目的稳定版本来处理一堆实验代码,但我仍然必须手动跟踪并修复我所做的所有更改。有什么想法或建议吗?

git checkout -b photo_tagging
git branch # to make sure it's right
# make a bunch of changes, creations, etc
git status # see what's changed since before
git add . # approve of the changes, I guess, since if I do git commit after this, it says no changes
git commit -m 'these are changes I made'

git checkout master
git branch #=> *master

# look at files, tags_controller is still there, added in photo_tagging
# and code added in photo_tagging branch are still there in *master

无论我是否在分支上进行提交,这似乎都会发生。

I really like git. At least, I like the idea of git. Being able to checkout my master project as a separate branch where I can change whatever I want without risk of screwing everything else up is awesome. But it's not working.

Basically, my workflow is like this:

  1. Checkout stable version to a new branch to experiment with new code
  2. Make a bunch of changes - I have no intent on keeping any of this, I'm just experimenting.
  3. Look at all the stuff I changed
  4. Add all the changes to be tracked
  5. Commit the branch and push branch to origin (if it worked, otherwise skip this step)
  6. Decide to try another method, go back to master
  7. Observe artifacts from the experimental branch I was working in, even though I'm in the master branch.

Every time I checkout a branch to another branch, make changes to the one branch, and then checkout the original branch, I still have all the files and changes that happened in the other branch. This is getting extremely frustrating. I've read that this can happen when you have files open in the IDE while doing this, but I've been pretty careful about that and both closed the files in the IDE, closed the IDE, and shut down my rails server before switching branches, and this still happens. Also, running 'git clean -f' either deletes everything that happened after some arbitrary commit (and randomly, at that), or, as in the latest case, didn't change anything back to its original state.

I thought I was using git correctly, but at this point, I'm at my wit's end here. I'm trying to work with a bunch of experimental code using a stable version of my project, but I keep having to manually track down and fix all the changes I made. Any ideas or suggestions?

git checkout -b photo_tagging
git branch # to make sure it's right
# make a bunch of changes, creations, etc
git status # see what's changed since before
git add . # approve of the changes, I guess, since if I do git commit after this, it says no changes
git commit -m 'these are changes I made'

git checkout master
git branch #=> *master

# look at files, tags_controller is still there, added in photo_tagging
# and code added in photo_tagging branch are still there in *master

This seems to happen whether I do a commit or not on the branch.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

蓬勃野心 2024-10-16 01:15:07

当我尝试将临时分支从主分支切换到另一个分支时,当我更改该临时页面上的某些内容并且不提交更改时,我会再次签出主分支,它实际上合并了来自临时分支的所有更改,我遇到了这个问题进入大师。

解答:

每当您签出 TEMP 分支时,请提交您的更改。这样,如果您将它们提交到 TEMP 分支并再次签出 MASTER,它将按预期工作。

I had this issue when I tried to switch temp to another branch from the master branch and when I would change something on that temp page and without committing the changes I would checkout the master branch again, it actually merged all the changes from the TEMP branch into MASTER.

ANSWER:

Whenever you checkout to a TEMP branch, COMMIT your changes. That way if you commit them on the TEMP branch and checkout MASTER again, it will work as supposed.

ˇ宁静的妩媚 2024-10-16 01:15:07

这是如何使用 git 和分支的示例。

$ git branch
* master
  organize

$ git branch networking
$ git checkout networking

$ git branch
  master
* networking
  organize

现在Master已经更新了很多次了,因为有人在网络上做了任何事情

$ git pull origin networking
From github.com:dlundquist/Asteroids
 * branch            networking -> FETCH_HEAD
CONFLICT (rename/delete): Rename src/ServerClientThread.java->src/ServerConnectionThread.java in 92d5b7c4e7c4110803eabf5d5152e7f2a337d722 and deleted in HEAD
Auto-merging src/Actor.java
Auto-merging src/Asteroids.java
CONFLICT (content): Merge conflict in src/Asteroids.java
Auto-merging src/BasicWeapon.java
CONFLICT (content): Merge conflict in src/BasicWeapon.java
CONFLICT (delete/modify): src/DedicatedServer.java deleted in HEAD and modified in 92d5b7c4e7c4110803eabf5d5152e7f2a337d722. Version 92d5b7c4e7c4110803eabf5d5152e7f2a337d722 of src/DedicatedServer.java left in tree.
Auto-merging src/MainMenu.java
CONFLICT (content): Merge conflict in src/MainMenu.java
CONFLICT (delete/modify): src/NetworkClientThread.java deleted in HEAD and modified in 92d5b7c4e7c4110803eabf5d5152e7f2a337d722. Version 92d5b7c4e7c4110803eabf5d5152e7f2a337d722 of src/NetworkClientThread.java left in tree.
CONFLICT (delete/modify): src/NetworkUpdate.java deleted in HEAD and modified in 92d5b7c4e7c4110803eabf5d5152e7f2a337d722. Version 92d5b7c4e7c4110803eabf5d5152e7f2a337d722 of src/NetworkUpdate.java left in tree.
Auto-merging src/ScenePanel.java
CONFLICT (content): Merge conflict in src/ScenePanel.java
Auto-merging src/Shield.java
CONFLICT (content): Merge conflict in src/Shield.java
Auto-merging src/Sprite.java
Auto-merging src/TripleShotWeapon.java
Auto-merging src/Weapon.java
Automatic merge failed; fix conflicts and then commit the result.
mjolnir:Asteroids Durandal$ git status
# On branch networking
# Changes to be committed:
#
#   modified:   src/Actor.java
#   modified:   src/Sprite.java
#   modified:   src/TripleShotWeapon.java
#   modified:   src/Weapon.java
#
# Unmerged paths:
#   (use "git add/rm <file>..." as appropriate to mark resolution)
#
#   both modified:      src/Asteroids.java
#   both modified:      src/BasicWeapon.java
#   deleted by us:      src/DedicatedServer.java
#   both modified:      src/MainMenu.java
#   deleted by us:      src/NetworkClientThread.java
#   deleted by us:      src/NetworkUpdate.java
#   both modified:      src/ScenePanel.java
#   added by them:      src/ServerConnectionThread.java
#   both modified:      src/Shield.java

哇,git真的很疯狂。看起来 Master 比 Networking 领先太多了,git 无法为我完成所有工作。

$ git reset --hard networking
HEAD is now at 20d6ee8 done tweaking game on the main branch.  Fixed a few bugs with the TripleShotWeapon.  Rebalanced the gameMechanics() in Asteroids to increase the difficulty as the levels increase.
mjolnir:Asteroids Durandal$ git status
# On branch networking
nothing to commit (working directory clean)

好吧,现在我“回到了过去”网络上次被推送到原点的时候。但我确实应该先回去与师父融合,然后再做任何事情。否则的话,合并就更困难了。

合并文件所花费的时间

$ git add (insert conflict resolved files here)
$ git commit -a -m "Merged networking with master"
$ git checkout master
$ git branch
* master
  networking
  organize

现在让我们应用我们的更改。

$ git merge networking
Already up-to-date.

如果您喜欢“是的!”,您可能会想要这样做。

$ git merge origin networking
Already up-to-date. Yeeah!

将我们的改变推向世界

$ git push origin master

Here is an example of how to use git and branches.

$ git branch
* master
  organize

$ git branch networking
$ git checkout networking

$ git branch
  master
* networking
  organize

Now Master has been updated many times since anyone has done anything on networking

$ git pull origin networking
From github.com:dlundquist/Asteroids
 * branch            networking -> FETCH_HEAD
CONFLICT (rename/delete): Rename src/ServerClientThread.java->src/ServerConnectionThread.java in 92d5b7c4e7c4110803eabf5d5152e7f2a337d722 and deleted in HEAD
Auto-merging src/Actor.java
Auto-merging src/Asteroids.java
CONFLICT (content): Merge conflict in src/Asteroids.java
Auto-merging src/BasicWeapon.java
CONFLICT (content): Merge conflict in src/BasicWeapon.java
CONFLICT (delete/modify): src/DedicatedServer.java deleted in HEAD and modified in 92d5b7c4e7c4110803eabf5d5152e7f2a337d722. Version 92d5b7c4e7c4110803eabf5d5152e7f2a337d722 of src/DedicatedServer.java left in tree.
Auto-merging src/MainMenu.java
CONFLICT (content): Merge conflict in src/MainMenu.java
CONFLICT (delete/modify): src/NetworkClientThread.java deleted in HEAD and modified in 92d5b7c4e7c4110803eabf5d5152e7f2a337d722. Version 92d5b7c4e7c4110803eabf5d5152e7f2a337d722 of src/NetworkClientThread.java left in tree.
CONFLICT (delete/modify): src/NetworkUpdate.java deleted in HEAD and modified in 92d5b7c4e7c4110803eabf5d5152e7f2a337d722. Version 92d5b7c4e7c4110803eabf5d5152e7f2a337d722 of src/NetworkUpdate.java left in tree.
Auto-merging src/ScenePanel.java
CONFLICT (content): Merge conflict in src/ScenePanel.java
Auto-merging src/Shield.java
CONFLICT (content): Merge conflict in src/Shield.java
Auto-merging src/Sprite.java
Auto-merging src/TripleShotWeapon.java
Auto-merging src/Weapon.java
Automatic merge failed; fix conflicts and then commit the result.
mjolnir:Asteroids Durandal$ git status
# On branch networking
# Changes to be committed:
#
#   modified:   src/Actor.java
#   modified:   src/Sprite.java
#   modified:   src/TripleShotWeapon.java
#   modified:   src/Weapon.java
#
# Unmerged paths:
#   (use "git add/rm <file>..." as appropriate to mark resolution)
#
#   both modified:      src/Asteroids.java
#   both modified:      src/BasicWeapon.java
#   deleted by us:      src/DedicatedServer.java
#   both modified:      src/MainMenu.java
#   deleted by us:      src/NetworkClientThread.java
#   deleted by us:      src/NetworkUpdate.java
#   both modified:      src/ScenePanel.java
#   added by them:      src/ServerConnectionThread.java
#   both modified:      src/Shield.java

Woah, git is really mad. It looks like Master is too far ahead of Networking for git to do all the work for me.

$ git reset --hard networking
HEAD is now at 20d6ee8 done tweaking game on the main branch.  Fixed a few bugs with the TripleShotWeapon.  Rebalanced the gameMechanics() in Asteroids to increase the difficulty as the levels increase.
mjolnir:Asteroids Durandal$ git status
# On branch networking
nothing to commit (working directory clean)

Ok, now I am "back in time" when networking last pushed to origin. But I really should go back and merge with Master now before I do any work. Other wise it will be even hard to merge.

Time spent merging files

$ git add (insert conflict resolved files here)
$ git commit -a -m "Merged networking with master"
$ git checkout master
$ git branch
* master
  networking
  organize

Now lets apply our changes.

$ git merge networking
Already up-to-date.

You might want to do this if you like the "Yeah!"

$ git merge origin networking
Already up-to-date. Yeeah!

Push our changes to the world

$ git push origin master
笛声青案梦长安 2024-10-16 01:15:07

我喜欢用通用语言解释 git。答案:这是因为假设更改分支(结帐)会将您带到另一个“工作空间”。 Git 不会这样做(虽然感觉更自然,而且很多人都有这样的误解)。

相反,可以说,分支就像标签。因此,您可以围绕分支跳舞,做一些工作,修改内容,再次跳舞,然后提交。然后,提交将位于您去的最后一个分支!结帐仅更改您将使用的“标签”/分支。

Martas 评论后更新

这适用于您的分支和主控同步(关于跟踪文件)或未跟踪文件的情况。如果实际上它们处于不同的状态,那么当您结账时,文件和文件夹实际上会消失并重新出现。如果您跟踪了更改但未提交,那么 git 实际上会中止签出,并且您必须提交或隐藏它们。

I like to explain git in common language. Answer: its because of the assumption that changing branches (checkout) will get you to another "workspace". Git doesn't do that (altough it feels more natural, and many have this misunderstanding).

Instead, branches are like tags, so to speak. So you can dance around branches, do some work, modify things, dance around again, and then commit. Then, the commit will be at the last branch you went to! The checkout only changes the "tag"/branch you'll use, period.

UPDATE after Martas's comment

This applies to when your branch and your master are synced (regarding tracked files), or with untracked files. If in fact they are in different states, when you checkout then files and folders will actually disapear and reappear. If you have changes tracked but not commited, then git will actually abort checking out, and you have to either commit or stash them.

半衬遮猫 2024-10-16 01:15:07

请检查您是否有写入权限。此错误是由于对存储库的只读访问而出现的。

Please check if you have write access or not. This error comes up with read only access to the repository.

苏辞 2024-10-16 01:15:07
$ git branch
* master
$ git checkout -b develop
$ git branch
  master
* branch

现在更改您想要的任何内容并执行 git add 。

$ git add .   <--Here is the source of the problem, always git add . at your branch to see the observable difference between two branches.
$ git commit -m "Some messages"

$ git checkout master
$ git branch
*master
develop

一切都整洁干净。

$ git branch
* master
$ git checkout -b develop
$ git branch
  master
* branch

Now change whatever you want and do git add .

$ git add .   <--Here is the source of the problem, always git add . at your branch to see the observable difference between two branches.
$ git commit -m "Some messages"

$ git checkout master
$ git branch
*master
develop

Everything is neat and clean.

无名指的心愿 2024-10-16 01:15:07

简而言之:提交您的更改(确保您在正确的分支“实验分支”上签出),即使您不想保留它,如果您想要的话,您可以稍后删除该分支)
然后签出您想要的分支(在更改之前),您的 IDE 现在将显示/更新您刚刚签出的分支中文件的状态)

in short: commit your changes(make sure you are checked out on the correct branch the "experimental one"), even if you dont care to keep it, you can delete the branch later if thats what you want)
then checkout the branch you want (before your changes), your IDE will now show/update to the status of the files in the branch you just checked out)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文