Skip to content

Instantly share code, notes, and snippets.

@rekomat
Last active September 30, 2022 07:12
Show Gist options
  • Save rekomat/6f8c1e48830aad0d61018b2f2eae9ea0 to your computer and use it in GitHub Desktop.
Save rekomat/6f8c1e48830aad0d61018b2f2eae9ea0 to your computer and use it in GitHub Desktop.

Git Cheatsheet

Remotes

Add a remote URL

git remote add origin git@bitbucket.org:user/repo-name.git

Changing a remote's URL

List your existing remotes in order to get the name of the remote you want to change.

$ git remote -v
origin	git@bitbucket.org:user/repo-name.git (fetch)
origin	git@bitbucket.org:user/repo-name.git (push)

Change your remote's URL

$ git remote set-url origin git@bitbucket.org:user/repo-name.git

Verify that the remote URL has changed with git remote -v (s. above).

Branches

Rename local branch

# Switch to the local branch you want to rename
$ git checkout <oldname>
# Rename local branch
$ git branch -m <newname>

Rename remote branch

# Push the local branch with the new name and reset the upstream branch
$ git push origin -u <newname>
# Delete the remote branch with the old name
$ git push origin --delete <oldname>

Push/create local branch to/on remote

Sometimes you want to explicitly declare the remote's branch name. This might be the case when the remote's branch name is different then the local or when working with several remotes.

git push <remote-name> <local-branch-name>:<remote-branch-name>

Stats

Number of commits (exclude merges) grouped by author over all branches

$ git shortlog -s -n --all --no-merges

Tags

Delete tag locally

$ git tag --delete <tagname>

Delete tag on remote

Push an empty reference to the remote tag name
$ git push origin <tagname>

Or delete the tag explicitly
$ git push --delete origin <tagname>

Bisect

# Start bisect process  
$ git bisect start
# Provide 'good' and 'bad' commit
$ git bisect bad HEAD
$ git bisect good 8341e03b
# Git will check out a commit in the given range. 
# Check the app/code and tell Git the result 
$ git bisect [good|bad]
# Stop the bisect process when you're done
$ git bisect reset

Convert SVN to Git

Convert a local svn repository

Create an authors file to map svn users (e.g. 'authors.txt')

user = User Name <user@domain.net>
john = John Doe <john@doe.com>
# Clone the repo
$ git svn clone --no-metadata -A ./authors.txt -t tags -b branches -T trunk file:///Users/username/my-svn-repo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment