Changes between Version 65 and Version 66 of DeveloperGuidelines/Git


Ignore:
Timestamp:
09/18/13 10:48:09 (8 years ago)
Author:
Pat Tressel
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DeveloperGuidelines/Git

    v65 v66  
    151151
    152152
    153 ==== Resetting to a previous commit ====
    154 There are two types of resets soft and hard.
    155 A soft reset is done like this:
    156 {{{
    157 git reset --soft HEAD@{N}
    158 }}}
    159 This resets your branch N previous commits (to a commit at a distance N from HEAD, the latest commit) but only in terms of git history. Hence your code remains untouched by git in a soft reset.
    160 
    161 A hard reset is done using:
    162 {{{
    163 git reset --hard HEAD@{N}
    164 }}}
    165 This resets your branch N previous commits removing both history and changes to the file ever since.
     153==== Undoing a commit ====
     154You may need to remove a commit you've made, either if you find you don't want it, or if it needs to be fixed. (Note if your commit has already been pushed to !GitHub and accepted into the trunk, then you should use a new commit to take out unwanted changes. This section only applies if you're fixing up your repository before the changes are in trunk.)
     155
     156If you want to fix up your latest commit, you can undo the commit, and unstage the files in it, by doing:
     157{{{
     158git reset HEAD~1
     159}}}
     160This will return your repository to its state before the git add commands that staged the files. Your changes will be in your working directory. HEAD~1 refers to the commit below the current tip of the branch.
     161
     162If you want to uncommit N commits, but keep the code changes in your working directory:
     163{{{
     164git reset HEAD~N
     165}}}
     166
     167If you want to get rid of your latest commit, and do not want to keep the code changes, you can do a "hard" reset.
     168{{{
     169git reset --hard HEAD~1
     170}}}
     171
     172Likewise, if you want to discard the last N commits, and do not want to keep the code changes:
     173{{{
     174git reset --hard HEAD~N
     175}}}
     176
     177If you are not entirely sure you want to discard the code changes, but need a branch without them, you can simply check out a new branch that ends at the last commit you want. E.g. to make a branch that ends N commits before the latest:
     178{{{
     179git checkout -b <new_branch_name> HEAD~N
     180}}}
     181See below for more on using multiple branches.
    166182 
    167 To remove commit from GitHub:
    168 {{{
    169 # Rebase locally (this allows you to remove the last commit)
    170 git rebase -i HEAD~2
    171 # Force update of GitHub
    172 git push origin +master
     183To remove a commit from your repository on !GitHub, use either the hard reset or new branch method above to reset your local repository to the commit you want on !GitHub. !GitHub will not let you overwrite a branch with a different history unless you tell it to with the --force option. If your local and remote branches have the same names, you only need to specify the branch name once, without the ":".
     184{{{
     185git push --force origin <local_branch_name>:<github_branch_name>
    173186}}}
    174187