Throw away local commits in Git

前端 未结 20 1614
遥遥无期
遥遥无期 2020-12-02 03:08

Due to some bad cherry-picking, my local Git repository is currently five commits ahead of the origin, and not in a good state. I want to get rid of all these commits and st

相关标签:
20条回答
  • 2020-12-02 03:40

    If you get your local repo into a complete mess, then a reliable way to throw away local commits in Git is to...

    1. Use "git config --get remote.origin.url" to get URL of remote origin
    2. Rename local git folder to "my_broken_local_repo"
    3. Use "git clone <url_from_1>" to get fresh local copy of remote git repository

    In my experience Eclipse handles the world changing around it quite well. However, you may need to select affected projects in Eclipse and clean them to force Eclipse to rebuild them. I guess other IDEs may need a forced rebuild too.

    A side benefit of the above procedure is that you will find out if your project relies on local files that were not put into git. If you find you are missing files then you can copy them in from "my_broken_local_repo" and add them to git. Once you have confidence that your new local repo has everything you need then you can delete "my_broken_local_repo".

    0 讨论(0)
  • 2020-12-02 03:42
    git reset --hard <SHA-Code>
    

    This will come in handy if you have made some mistakes on your local copy that you want to make sure doesn't get pushed to your remote branch by mistake.

    The SHA-Code can be obtained by looking at webVersion of your git dashboard for the last commit on the branch.

    This way you can get synchronized with the last commit on the branch.

    You can do git pull after you have successfully completed the hard reset to confirm nothing new to syn i.e. you get to see the message.

    Your branch is up to date with Origin/<Branch Name>

    0 讨论(0)
  • 2020-12-02 03:45

    If you are using Atlassian SourceTree app, you could use the reset option in the context menu.

    enter image description here

    0 讨论(0)
  • 2020-12-02 03:46

    Find the sha1 for the commit you want to revert to:

    za$ git reflog
    ... snip ...
    cf42fa2... HEAD@{0}: commit: fixed misc bugs
    ~
    ~
    cf42fa2... HEAD@{84}: commit: fixed params for .....
    73b9363... HEAD@{85}: commit: Don't symlink to themes on deployment.
    547cc1b... HEAD@{86}: commit: Deploy to effectif.com web server.
    1dc3298... HEAD@{87}: commit: Updated the theme.
    18c3f51... HEAD@{88}: commit: Verify with Google webmaster tools.
    26fbb9c... HEAD@{89}: checkout: moving to effectif
    

    And then use --mixed flag so that you "reset HEAD and index":

    za$ git reset --mixed cf42fa2
    

    Available flags:

    za$ git reset -h
    
    -q, --quiet           be quiet, only report errors
    --mixed               reset HEAD and index
    --soft                reset only HEAD
    --hard                reset HEAD, index and working tree
    --merge               reset HEAD, index and working tree
    --keep                reset HEAD but keep local changes
    --recurse-submodules[=<reset>]
                          control recursive updating of submodules
    -p, --patch           select hunks interactively
    -N, --intent-to-add
    
    0 讨论(0)
  • 2020-12-02 03:47

    If your excess commits are only visible to you, you can just do git reset --hard origin/<branch_name> to move back to where the origin is. This will reset the state of the repository to the previous commit, and it will discard all local changes.

    Doing a git revert makes new commits to remove old commits in a way that keeps everyone's history sane.

    0 讨论(0)
  • 2020-12-02 03:47

    On your branch attempt:

    git reset --hard origin/<branch_name>
    

    Validate the reversal (to the state, with no local commits), using "git log" or "git status" hence.

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