git bash - Various ways to remove local Git changes -
i cloned git repository , checked out branch. worked on it, , decided remove local changes, wanted original copy.
in short, had following 2 commands remove local changes
git checkout . git clean -f
my question is,
(1) correct approach in getting rid of local changes, or else please let me know correct approach.
(2) when use git reset --hard
able reset without command
thanks
*solution : major edit(s): 03/26 : * replaced many of vague terms git specific terminology [tracked/untracked/staged/unstaged]
there 3 categories of files when make local changes:
type 1. staged tracked files
type 2. unstaged tracked files
type 3. unstaged untracked files a.k.a untracked files
- staged - moved staging area/ added index
- tracked - modified files
- untracked - new files. unstaged. if staged, means tracked.
what each commands do:
git checkout .
- removes unstaged tracked files [type 2]git clean -f
- removes unstaged untracked files [type 3]git reset --hard
- removes staged tracked , unstaged tracked files only[type 1, type 2]git stash -u
- removes changes [type 1, type 2, type 3]
conclusion:
it's clear can use either
(1) combination of `git clean -f` , `git reset --hard`
or
(2) `git stash -u`
to achieve desired result.
note: stashing, word means 'store (something) safely , secretly in specified place.' can retrieved using git stash pop
. choosing between above 2 options developer's call.
thank christoph , frederik schøning.
edit: 03/27
i thought it's worth putting 'beware' note git clean -f
git clean -f
there no going back. use -n
or --dry-run
preview damage you'll do.
if want remove directories, run git clean -f -d
if want remove ignored files, run git clean -f -x
if want remove ignored non-ignored files, run git clean -f -x
reference : more on git clean
: how remove local (untracked) files current git working tree?
edit: 05/20/15
discarding local commits on branch [removing local commits]
in order discard local commits on branch, make local branch identical "upstream" of branch, run git reset --hard @{u}
reference: http://sethrobertson.github.io/gitfixum/fixup.html
or git reset --hard origin/master
[if local branch master
]
note: 06/12/2015 not duplicate of other question that's marked duplicate. question address how remove local git changes [remove file added, remove changes added existing file etc , various approaches; in other thread address how remove local commit. if added file, , want remove alone, other thread doesn't discuss it. hence not duplicate of other one]
edit: 06/23/15
how revert commit pushed remote repository?
$ git revert ab12cd15
edit: 09/01/2015
delete previous commit local branch , remote branch
case: commited change local branch , pushed remote branch, realized , oh no! dont need change. what?
git reset --hard head~1
[for deleting commit local branch]
git push origin head --force
[both commands must executed. deleting remote branch]
whats branch ? checked out branch.
edit 09/08/2015 - remove local git merge:
i on master
branch , merged master
branch newly working branch phase2
$ git status # on branch master $ git merge phase2 $ git status # on branch master # branch ahead of 'origin/master' 8 commits.
q: how rid of merge? tried git reset --hard
, git clean -d -f
both didn't work.
the thing worked of below ones:
$ git reset --hard origin/master
or
$ git reset --hard head~8
or
$ git reset --hard 9a88396f51e2a068bb7
[sha commit code - 1 present before merge commits happened]
it depends on trying undo/revert. start out reading the post in ube's link. attempt answer:
hard reset
git reset --hard [head]
completely remove staged , unstaged changes tracked files.
i find myself using hard resetting, when i'm "just undo if had done complete re-clone remote". in case, want repo pristine, work.
clean
git clean [-f]
remove files not tracked.
for removing temporary files, keep staged , unstaged changes tracked files. times, end making ignore-rule instead of repeatedly cleaning - e.g. bin/obj folders in c# project, want exclude repo save space, or that.
the -f (force) option remove files, not tracked and being ignored git though ignore-rule. in case above, ignore-rule never track bin/obj folders, though these folders being ignored git, using force-option remove them file system. i've sporadically seen use this, e.g. when scripting deployment, , want clean code before deploying, zipping or whatever.
git clean not touch files, being tracked.
checkout "dot"
git checkout .
i had never seen notation before reading post. i'm having hard time finding documentation (maybe can help), playing around bit, looks means:
"undo changes in working tree".
i.e. undo unstaged changes in tracked files. apparently doesn't touch staged changes , leaves untracked files alone.
stashing
some answers mention stashing. wording implies, use stashing when in middle of (not ready commit), , have temporarily switch branches or somehow work on state of code, later return "messy desk". don't see applies question, it's handy.
to sum up
generally, if confident have committed , maybe pushed remote important changes, if playing around or like, using git reset --hard head
followed git clean -f
definitively cleanse code state, in, had been cloned , checked out branch. it's important emphasize, resetting remove staged, uncommitted changes. it wipe has not been committed (except untracked files, in case, use clean).
all other commands there facilitate more complex scenarios, granularity of "undoing stuff" needed :)
i feel, question #1 covered, lastly, conclude on #2: reason never found need use git reset --hard
had never staged anything. had staged change, neither git checkout .
nor git clean -f
have reverted that.
hope covers.
Comments
Post a Comment