I think, therefore I am. I am, therefore I sail

Category: OS X Page 3 of 6

Remove untracked local files from Git

If you have need to get rid of a bunch of files that are in your git repo but not yet tracked it can be tedious to do the job one file at a time. Git has you covered with the git clean command.

Warning: This is a permanent loss of files. Always use –dry-run to verify the action.

Remove untracked files

Remove all untracked files with

[bash]git clean -f[/bash]

Remove untracked files and directories

Remove all untracked files and also any untracked directories

[bash]git clean -f -d[/bash]

Cleanup and remove all merged local and remote git branches

After working on a project for a while you wind up with lots of feature and hotfix branches. If you are working on a team with a remote cannonical project repo there will likely be dozens or hundreds of stale branches that need pruning. Here are a few quick commands that will help you clean up your branches.

TL;DR – Just give me the code!

Get a list of remote branches

[bash]for remote in `git branch -r `; do git branch –track $remote; done[/bash]

Remove remote branches

[bash]git branch –merged master | grep -v master | cut -d/ -f2- | xargs -n 1 git push –delete origin[/bash]

Remove local branches

[bash]git branch –merged master | grep -v master | xargs -n 1 git branch -d[/bash]

Order is important

The order that you remove branches in is important. You must remove the remote branches first otherwise git will not know how to reference the names of the remote branches and they will remain.

Get a list of branches

Chances are if you are working with a team you will not have all of the remote repo branches locally. In order to remove the remote branches your local git needs to know about them. This command will pull a list of the remote branches without fully checking them out. You do not need the contents of the branch, only the name for reference.

[bash]for remote in `git branch -r `; do git branch –track $remote; done[/bash]

Note: While writing this I realized there may be a more efficient way to do this by passing the branch name directly into git’s delete function. I will test this in the future on another cleanup.

Remove merged remote branches

Remote branches that have been merged into master should generally be safe to remove and can easily be accomplished via:

[bash]git branch –merged master | grep -v master | cut -d/ -f2- | xargs -n 1 git push –delete origin[/bash]

Per command breakdown

Get a list of all branches that have been merged into the master branch:

[bash]git branch –merged master[/bash]

Just to be safe cut out the master branch from the previous output if it exists:

[bash]grep -v master[/bash]

When you ran the command to pull a list of remote branches without checking them out is probably added ‘origin/‘ into the branch name. This will confuse the remote repo during the delete operation. The following removes origin/ from the branch name before passing it to the delete command:

[bash]cut -d/ -f2-[/bash]

Finally you can call the git command to delete the remote branch. The xargs command passes the branch names with their manipulations to the git command:

[bash]xargs -n 1 git push –delete origin[/bash]

Remove merged local branches

Local branches that have been merged into master should generally be safe to remove and can easily be accomplished via:

[bash]git branch –merged master | grep -v master | xargs -n 1 git branch -d[/bash]

Per command breakdown

Get a list of all branches that have been merged into the master branch:

[bash]git branch –merged master[/bash]

Just to be safe cut out the master branch from the previous output if it exists:

[bash]grep -v master[/bash]

Finally you can call the git command to delete the remote branch. The xargs command passes the branch names with their manipulations to the git command:

[bash]xargs -n 1 git branch -d[/bash]

Recursively count all files in a directory from the terminal

If you utilize the terminal on a regular daily basis like I do you are bound to run into times when you need to get a count of how many files exist in a directory structure. Here is some code that will display that count for you.

find . -type f | wc -l

The ‘.’ in the command above refers to the current directory. If the directory you are inspecting exists elsewhere replace ‘.’ with the path to that directory.

Quick and easy way to show the current Git branch on your command prompt

If you work with lots of branches in Git it is nice to be able to see at a glance which branch you are on instead of typing git branch each time. Here are 4 quick lines you can add to your bash prompt that will show the current branch.

You will need to edit your user bash config which can be in different locations depending on your operating system.

  • OS X: ~/.profile
  • Linux General: ~/.bash_profile

~ represents the path to your user home directory. For my user, blobaugh, on OS X the path is /Users/home/blobaugh.

Add in the file the following lines:

parse_git_branch() {
       git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/ (1)/'
}
export PS1="u@h W[33[31m]$(parse_git_branch)[33[00m] $ "

Restart your terminal and change directories to your repository and you should now see the branch in parenthesis at the end of your prompt.

Screen Shot 2014-01-14 at 3.55.32 PM

Add or Update your vSphere or ESXi license key through the terminal

If you are like me and you forgot to input your vSphere or ESXi license key before it expired not to fear! It is possible to update the license key through the terminal.

The license is stored in a plain text file at /etc/vmware/vmware.lic. To edit it from the terminal type in:

vi /etc/vmware/vmware.lic

Erase the code that is in there and put in yours.

Now the hard part, getting it to apply. It seems that restarting the management service should do it, but this has changed between versions of vSphere and ESXi. I simply rebooted the host machine and everything worked fine. It can be accomplished with the shutdown.sh command.

Page 3 of 6

Powered by WordPress & Beards