Git commands I keep forgetting (part 2)

One year ago I wrote a blog about the Git commands I keep forgetting and Update local develop branch without checkout. These blogs contains some of the commands that I frequently use during developing. After writing this first blog post about git commands I learned other git commands as well that I find interesting to share with you.

Remove local branches when remote branch is already removed

In the projects I currently work on we use Git Flow a lot. This means that we use a lot of branching and when a remote branch is removed (ex: git flow feature finish) by someone else my own local git repo must be updated. You can delete the remote branches from the cache using the fetch prune command:

git fetch -p

But if you have pulled the branch ones then you also have a local branch. When you use this command the local branch is not deleted. For deleting local branches use the following command:

git branch -d some_feature_branch

If the local branch has some changes that where not merged to the master or develop branch git will show a warning. To force the branch deletion use:

git branch -D some_feature_branch

*note: the uppercase -D instead of -d

Remove remote branch

Sometimes it happens that the remote branch is not correctly removed from the remote repository during 'git flow feature finish'. I you want to delete the remote branch using the command line use the following command:

!!!!CAUTION NO UNDO, USE AT OWN RISK!!!

git push origin --delete some_feature_branch

Temporarily store changes without commit

If you have made changes but suddenly you see you are working on the wrong branch. Oops... What to do now? Stash comes to rescue you.
With stash you can set changes in a temporary storage of git and pull the changes when needed. So when you have changes on some branch and want to move them to another branch you can use the following commands:

# Stash the uncommited changes
git stash

# Swith to other branch
git checkout some_other_branch

# Get the changes from the stash
git stash pop

There is a lot you can do with stashing so for the full documentation go to https://git-scm.com/docs/git-stash

 

Happy Coding!