Question

I want a single git command that does this: no matter what state I am in - I might have local unpushed commit, or unstaged commits or things in my changelist or I might be in the middle of a rebase or merge or I am in some weird state, it does this:

  1. Deletes all untracked files, resets if I was in middle of rebase/merge, nukes all unpushed commits, reverts all my local changes etc etc
  2. Resets me to the HEAD of remote of that branch

Every once in a while, I get myself into a situation where I just want to say "Ok git, please put me back in a state which looks exactly as I did a fresh clone of the repo and did a git checkout -b of the branch I am (or was if I am in middle of rebase) on"

Was it helpful?

Solution 3

Found exactly what I was looking for: https://github.com/mapmeld/gitjk

OTHER TIPS

There is no "1 answer" solution because the number of possibilities is large and what is good for one situation is not good for another situation. So, as @twalberg notes, you should learn the various commands and what does what and why.

Having said that.... I usually use the following:

git reset --hard HEAD # removes whatever I've done locally and go back to using the version of the branch that I had originally pulled (or since my last commit). Other developers also do git reset --hard origin/the_branch_name to do this.

git checkout -b new_branch # Do this for new branches AND when you've made a bunch of changes to a branch, then realized that you had meant to do this work in a new branch. ALSO before doing merges and rebases I have learned that is good to do a checkout -b new_new_branch_name from your new_branch to do the rebasing and merging in. That way if it gets screwed up you can just delete that branch and start again with no ill effect.

git rebase -i HEAD~10 means interactively rebase the past 10 commits. This means make changes to the historical commits that were made (the current state itself doesn't change).
This brings up an editor that allows you to squash, reorder or delete commits in your commit history. This can be an extremely useful command for tidying up lots of different changes in different commits to be grouped in fewer commits. We always try to squash (if needed) before code review.
Caveats: Interactive rebase is best used for branches not yet pushed to remotes, especially if there are other collaborators (you can mess their history up if not careful). Basically it's best to use when you're either still local, or you have pushed to remote but you know that no-one else has worked on that branch.

git checkout path_to/and_file_at/myfile.rb This was revert a single file to whatever state it was in at the last commit. Basically it throws away your local (uncommitted) changes.

git clean - Recursively removes files that aren't under version control, starting from the current directory.

You want git reset, see this page for examples on what you want to do.

As pointed out in the comments, git clean will remove untracked files from the tree, see this page for details on how that works.

You could write an alias or something to do these two commands together if you really only want it to be a single command.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top