Skip to content

Instantly share code, notes, and snippets.

@nlitwin
Last active September 3, 2019 17:19
Show Gist options
  • Save nlitwin/931b0cbbdf323fe795a2 to your computer and use it in GitHub Desktop.
Save nlitwin/931b0cbbdf323fe795a2 to your computer and use it in GitHub Desktop.
Useful Git Commands
# Revert to a previous commit
git reset --hard commit_sha
# Create a local branch which tracks from an already created remote branch
git co --track -b branch_name origin/branch_name
# Push local branch to a remote branch with a different name
git push origin local-name:remote-name
# If you want to rename a branch while pointed to any branch
git branch -m <oldname> <newname>
# If you want to rename the current branch
git branch -m <newname>
# List all remote branches
git branch --remote --list
# If repository name changes, change the remote origin
git remote
git remote show origin
git remote rm origin
git remote add origin https://github.com/user/repo_name.git
# See diff between two branches
git diff branch_1..branch_2
# That will produce the diff between the tips of the two branches.
# If you'd prefer to find the diff from their common ancestor to test, you can use three dots instead of two:
git diff branch_1...branch_2
# Search content of old commits
git grep <regexp> $(git rev-list --all -- my/sub/directory) -- my/sub/directory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment