Git commands I keep forgetting

(see also Part 2)

Most of the time that I use Git I will use it from inside Visual Studio. But sometimes it is needed to use the console window for git commands. In this post I will show some git command I keep forgetting myself.

Show remote branches

If you use Visual Studio 2015 remote branches are available in the IDE, but when using Visual Studio 2013 this is not the case. If you want to check which branches are remote available use te following command:

git remote show origin

Remove unpushed commits

Sometimes it happens that I already committed my changes but don't want to push the commits to the remote branch because change of directions. One possible thing to do is to revert the commit, but this results in 2 commits one with changes and one with the reverted changes. I personally don't like the fact to have commits for something I don't want. To actually remove the commits you can use the following command:

git reset --hard origin/master

Remove deleted branches from local cache 

Sometimes it happens that a branch is already removed from the remote git but is still in your git list when executing the 'remote show origin' action. When working with GitFlow for example it happens a lot the Visual Studio still shows the remote feature branches even when they are already finished. To clear the list en remove the branches that are not available any more use the following command:

git fetch -p

Credentials asked every time you pull from git 

If you use Git from the command line frequently it will ask your credentials every time you execute the 'git pull' command. To avoid the problem of entering your credentials every time, you can store the credentials in the credential helper using the following command:

git config --global credential.helper "cache --timeout=3600"
git config --global credential.helper wincred

When you perform a git pull git wil ask the credentials one time and will store them in the credential cache.

 

There are a lot more Git commands, but the commands I mentioned above are the command that I forget all the time. Hopefully this can help you as well.