GIT Simplified removal of 'stale' remote branches and their local branches

As you may noticed in my previous posts I use Git in a lot of projects I am working on.

Previous posts:

Git commands I keep forgetting

Update local develop branch without checkout

Git commands I keep forgetting part 2

In the last one I already talked about "Remove local branches when remote branch is already removed". I used this way of removing a lot the last couple of months. But even with these simple command it is still a lot of work to get your local copy clean.

During some searching on the internet I found another great way to make it even simpeler to cleanup the local repository.

List branches that have no remote branch anymore.

git branch -vv

The above command can be used to list branches in a verbose way. And the output will be something like this:

bugfix/BF_Bug1           6a64538 [origin/bugfix/BF_Bug1: gone] Fixed bug1
bugfix/BF_Bug2           b7cf3ea [origin/bugfix/BF_Bug2] Fixed bug2
* develop                ca60d674 [origin/develop] Merged PR 91: Added Feature
feature/SomeGreatFeature ccb8b3f [origin/feature/SomeGreatFeature: gone] Some great feature added
master                   10ed942 [origin/master] Updated README.md

As you can see 2 branches are 'gone', which means the remote branch does not exist anymore. Now you know which branches you can remove from the local repository with the commands:

git branch -d bugfix/BF_Bug1

git branch -d feature/SomeGreatFeature

The above commands are not so hard to learn, but if you have a lot of branches that you want to remove it can be time consuming to remove every branch manualy. To make your life a little bit easier you have to do the following steps:

  1. Find you .gitconfig file which contains the configuration of GIT.
    On my machine the .gitconfig file could be found at: %USERPROFILE%\.gitconfig
  2. It is possible to add alias command in this configuration file and that's excactly what we are going to do. Add the following lines to the .gitconfig file:
    [alias]
    prune-branches = !git remote prune origin && git branch -vv | grep ': gone]' | awk '{print $1}' | xargs -r git branch -d
  3. You can now remove all local branches from which the remote branch is removed. Open a command prompt in you source directory and type the following command:
    git prune-branches
  4. The output will be something like:
    Deleted branch bugfix/BF_Bug1 (was 6a64538).
    
    Deleted branch feature/SomeGreatFeature (was ccb8b3f).

I really like this command, because now I can execute one command that will clean my local repository. Of course you can name the alias whatever you like. Personally I updated my alias to be ' git pb'  which is faster to type than 'git prune-branches'.

I hope I have made you life a little easier by writing this blog!

Happy Coding!

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!

 

Start TFS vNEXT (2015) build for a specific commit using GIT

Today I was trying to start a TFS build with a specified commit id using the "QUEUE NEW BUILD" windows of TFS vNext.

But when trying to do so TFS reports the following problem: "The value specified for SourceVersion is not va valid commit id"

 

After a lot of searching on the internet I found the problem dat TFS vNEXT uses the SHA1 version of the commit id. To get the SHA1 commit id you can execute the following command:

git rev-parse HEAD

This command returns the latest commit id on HEAD and returns this in the SHA1 format:

c7ab3fd13e30b55394afc3ebb5c73240c662b160

Using this SHA1 version of commit id in the TFS windows will start the Build for the specified commit. If you want to start a build for an older commit perform the following steps:

  1. Find the Commit id from TFS (ex: a9d8f7e7)
  2. Open the Command prompt and type 
    git rev-pars a9d8f7e7
  3. Output of the command should be something like: a9d8f7e76d24d5b4f1cc15378eb9513a6e2cdb4d
  4. Copy the SHA1 commit id
  5. Queue new build in TFS
  6. Past the output of the git command in the "Commit" textbox

Hopefully Microsoft will add some support for the smaller commit id's in the future, but for now this can be the workaround.

Update (Thanks to Travis and Artour)

There is a way to get the CommitID using Visual Studio. If you open the History from a branch and open de Commit Details of a commit you can press the 'Actions' link and choose 'Copy Commit ID'

If you are using SourceTree you see the full CommitId.

 

Happy coding ;-)

Update local develop branch without checkout

Early this year I wrote a post about Git Command that I think are handy to know: Git commands I keep forgetting

After working with Git more and more I have gathered some other command as well that I would like to share with you:

Update the local develop branch while working on a feature branch

We use git flow from nvie a lot. Which is very nice because it handles a lot of stuff for you. But one thing that I did not like was when I wanted to finish the feature using: 

$ git flow feature finish FEATURE-NAME

I first had to update the develop branch because otherwise it will merge with an old version of develop. Steps that I had to follow where:

# switch to develop
$ git checkout develop

# update develop
$ git pull

# switch to feature branch
$ git checkout feature/FEATURE-NAME

# merge from develop to feature branch
$ git merge develop

# finish feature
$ git flow feature finish FEATURE-NAME

# push changes
$ git push

It is also possible to merge from origin/develop to your feature branch, but still if you want to finish the feature branch you have to make sure that the local develop branch is up to date.

With the following command you can eliminate the first 3 commands:

# update develop branch without checkout
$ git fetch origin develop:develop

So if the feature branch is not up to date with develop the flow becomes the following:

# update develop
$ git fetch origin develop:develop

# merge from develop to feature branch
$ git merge develop

# finish feature
$ git flow feature finish FEATURE-NAME

# push changes
$ git push

 

 

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.