This document provides a few shortcuts to Git commands.
git config --get remote.origin.url
Or a quicker, more verbose method
git remote -v
git remote set-url origin [email protected]
We can verify the changes with
git remote -v
git checkout -b branchname
git checkout branchname
git checkout path/to/file
To discard all unstaged changes
git checkout -- .
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".
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
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
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
git tag --delete tagname
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.
To add current work in progress, we just need the following
git stash
git stash list
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}
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
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
git revert HEAD
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
To view the changes log of a single file we can do the following
git log --follow -p -- filepath
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]
Development of new features start from the 'develop' branch.
git flow feature start feature_name
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 to the remote server so it can be used by other users.
git flow feature publish feature_name
Get a feature published by another user.
git flow feature track feature_name