Skip to content

Instantly share code, notes, and snippets.

@netoxico
Last active October 10, 2015 09:08
Show Gist options
  • Save netoxico/3667187 to your computer and use it in GitHub Desktop.
Save netoxico/3667187 to your computer and use it in GitHub Desktop.
Git Notes

Git Notes

Create a new branch from develop remote branch

git checkout -b BID-XXX origin/develop

//Do Development here.

git checkout develop // Switch branch to'develop'

git merge --no-ff BID-XXX // Update

git branch -d BID-XXX // Deleted branch BID-XXX

git push origin develop // Push

Delete remote branch

git push origin :ui-r19

Add a remote branch

git push origin new_remote_branch

Rename branch

git branch -m old_branch new_branch

Remove all new added files

git rm -r --cached .

Revert to a commit by SHA hash

# reset the index to the desired tree
git reset 56e05fced

# move the branch pointer back to the previous HEAD
git reset --soft HEAD@{1}

git commit -m "Revert to 56e05fced"

# Update working copy to reflect the new commit
git reset --hard

Revert

This depends a lot on what you mean by "revert".

If you want to temporarily go back to it, fool around, then come back to where you are, all you have to do is check out the desired commit:

this will detach your HEAD, i.e. leave you with no branch checked out.

git checkout 0d1d7fc32 or if you want to make commits while you're there, go ahead and make a new branch while you're at it:

git checkout -b old-state 0d1d7fc32 If, on the other hand, you want to really get rid of everything you've done since then, there are two possibilities. One, if you haven't published any of these commits, simply reset:

This will destroy any local modifications

Don't do it if you have uncommitted work you want to keep

git reset --hard 0d1d7fc32

Alternatively, if there's work to keep:

git stash git reset --hard 0d1d7fc32 git stash pop

This saves the modifications, then reapplies that patch after resetting.

You could get merge conflicts, if you've modified things which were

changed since the commit you reset to

On the other hand, if you've published the work, you probably don't want to reset the branch, since that's effectively rewriting history. In that case, you could indeed revert the commits. With git, revert has a very specific meaning: create a commit with the reverse patch to cancel it out. This way you don't rewrite any history.

This will create three separate revert commits

git revert 0766c053 25eee4ca a867b4af

To get just one, you could use rebase -i to squash them afterwards

Or, you could do it manually (be sure to do this at top level of the repo)

get your index and work tree into the desired state, without changing HEAD:

git checkout 0d1d7fc32 .

and then commit

git commit # be sure and write a good message describing what you just did The git-revert manpage actually covers a lot of this in its description. Another useful link from the Git Community Book discussing git-revert is here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment