Skip to content

Instantly share code, notes, and snippets.

@goncalo-oliveira
Last active February 6, 2023 09:18
Show Gist options
  • Save goncalo-oliveira/4613b663765fb09f5138f07006a66dd9 to your computer and use it in GitHub Desktop.
Save goncalo-oliveira/4613b663765fb09f5138f07006a66dd9 to your computer and use it in GitHub Desktop.
Git Cheat Sheet

Git Cheat Sheet

This document provides a few shortcuts to Git commands.

Config

Retrieve origin url

git config --get remote.origin.url

Or a quicker, more verbose method

git remote -v

Update origin url

git remote set-url origin git@repository.git

We can verify the changes with

git remote -v

Branches

Create a new branch from current

git checkout -b branchname

Switching branches

git checkout branchname

Discarding changes

git checkout path/to/file

To discard all unstaged changes

git checkout -- .

Deleting branches

To delete a local branch

git branch -d branchname

To delete a remote branch

git push origin --delete branchname

or the shorter syntax

git push origin :branchname

To remove local branches that no longer exist on the remote, we can synchronize the branch list

git fetch -p

The -p argument means "prune".

Tags

Create a new tag

Git supports two different types of tags: lightweight and annotated. The difference between both is that lightweight tags are just pointers to a commit, very much like a branch; annotated tags include details on when and who created it. Unless we are creating a temporary tag, it is recommended to always create annotated tags.

git tag -a v1.4.3

We can also include a message.

git tag -a v1.4.3 -m "Tagged version 1.4.3"

We can also specify a commit identifier, if we want to tag on a previous commit.

git tag -a v1.4.3 3f42072

Pushing tags

Tags are not pushed by default when you do a git push. Instead, we need to explicitly push them.

git push origin v1.4.3

Delete a remote tag

git push --delete origin tagname

Please keep in mind that tags and branches have a namespace so that you can use the same name for a branch and a tag. If this is the case, make sure you specify the full ref to only delete the tag.

git push origin :refs/tags/tagname

Delete a local tag

git tag --delete tagname

Stashes

Stashes are a good way to put some work in progress aside before moving to a different branch. Another reason to use a stash is if we started working on something on a different branch and we want to port that safely.

Add to stash

To add current work in progress, we just need the following

git stash

Listing stashes

git stash list

Applying a stash

To apply a stash to our current branch, we just need

git stash apply

Keep in mind that this will only apply the last stash. If you have multiple stashes and want to apply a particular one other than the last, you can use

git stash apply stash@{2}

Screw ups

Deleting a remote file

The file .gitignore only applies to files that aren't already tracked. If we need to remove tracking from a file, we need first to unstage it, commit and then push.

git rm --cached some-file
git commit -m 'Removed the now ignored file some-file'
git push origin master

We can do the same thing for an entire directory.

git rm -r --cached some-directory
git commit -m 'Removed the now ignored directory some-directory'
git push origin master

Revert a local commit

git reset --soft HEAD^    # Use --soft if you want to keep your changes
git reset --hard HEAD^    # Use --hard if you don't care about keeping the changes you made

Revert a remote commit

git revert HEAD

Revert last two commits or sequence of commits

Use the followiing, where commit1 is the hash of the first (oldest) commit and commit2 is the hash of the second (newest) commit.

git revert commit1^..commit2

Git Logs

To view the changes log of a single file we can do the following

git log --follow -p -- filepath

Git Flow

Initializing

Before using Git Flow the repository needs to be initialized. The conventions for Git Flow are as follows

git flow init
No branches exist yet. Base branches must be created now.
Branch name for production releases: [master]
Branch name for "next release" development: [develop]

How to name your supporting branch prefixes?
Feature branches? [] feature/
Bugfix branches? [] bugfix/
Release branches? [] release/
Hotfix branches? [] hotfix/
Support branches? [] support/
Version tag prefix? []
Hooks and filters directory? [C:/Users/username/repos/path/project/.git/hooks]

Start a new feature

Development of new features start from the 'develop' branch.

git flow feature start feature_name

Finish a feature

Finish the development of a feature. This action performs the following actions

  • Merges feature into 'develop'
  • Removes the feature branch
  • Switches back to 'develop' branch
git flow feature finish feature_name

Publish a feature

Publish a feature to the remote server so it can be used by other users.

git flow feature publish feature_name

Get a published feature

Get a feature published by another user.

git flow feature track feature_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment