Git checkout does not change anything

前端 未结 4 518
暗喜
暗喜 2020-12-30 18:47

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 screw

相关标签:
4条回答
  • 2020-12-30 18:52

    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.

    0 讨论(0)
  • 2020-12-30 19:08

    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
    
    0 讨论(0)
  • 2020-12-30 19:09
    $ 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.

    0 讨论(0)
  • 2020-12-30 19:14

    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.

    0 讨论(0)
提交回复
热议问题