我真的很喜欢git。至少,我喜欢git的想法。能够将我的主项目结账为一个单独的分支,在那里我可以改变我想要的任何东西,而不用担心其他任何事情都搞砸了,这真是太棒了。但它不起作用。
基本上,我的工作流程是这样的:
每次我将分支签出到另一个分支,对一个分支进行更改,然后签出原始分支,我仍然拥有在另一个分支中发生的所有文件和更改。这变得非常令人沮丧。我已经读过,当你在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
无论我是否在分支上进行提交,这似乎都会发生。
答案 0 :(得分:17)
当我尝试将temp从主分支切换到另一个分支时,我遇到了这个问题,当我在那个临时页面上更改某些内容而没有提交更改时,我会再次检查主分支,它实际上合并了所有更改来自TEMP分支进入MASTER。
答案:
每当您结帐到TEMP分支时,请执行更改。这样,如果你在TEMP分支上提交它们并再次检出MASTER,它将按预期工作。
答案 1 :(得分:14)
以下是如何使用git和branches的示例。
$ 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已经远远领先于网络,因为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
答案 2 :(得分:3)
我喜欢用通用语言解释git。答案:因为假设更改分支(结账)会让您进入另一个“工作区”。 Git不这样做(尽管感觉更自然,但很多人都有这种误解)。
相反,分支就像标签,可以这么说。所以你可以在树枝上跳舞,做一些工作,修改东西,再次跳舞,然后提交。然后,提交将在你去的最后一个分支!结帐只会更改您将使用的“标记”/分支,期限。
Martas发表评论后更新
这适用于您的分支和主服务器同步(关于跟踪文件)或未跟踪文件的情况。如果实际上它们处于不同的状态,当您结帐时,文件和文件夹实际上会消失并重新出现。如果你跟踪但没有提交更改,那么git实际上会中止检出,你必须提交或存储它们。
答案 3 :(得分:0)
$ 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
一切都干净整洁。
答案 4 :(得分:0)
简而言之:提交您的更改(确保您在正确的分支上签出“实验性分支”),即使您不想保留它,如果这是您想要的,您也可以稍后删除该分支) 然后检出您想要的分支(在您更改之前),您的 IDE 现在将显示/更新到您刚刚检出的分支中文件的状态)