Showing posts with label git. Show all posts
Showing posts with label git. Show all posts

Tuesday, August 21, 2018

git bash - deleted local branches

You can find out which branches have been merged into a branch by running the below:

$ git branch --merged origin/master
* master
  feature/some-branch
  feature/some-other-branch

This will also print out master as part of it, so we can then pipe that into grep to match results without master:

$ git branch --merged origin/master | grep -v master
  feature/some-branch
  feature/some-other-branch

Obviously, you won't want the name master in your branch name, otherwise it'll be excluded too. You can fancier with regex here.

Now we can take this result and pipe it into xargs, to run git branch -d passing the output as arguments

$ git branch --merged origin/master
  | grep -v master
  | xargs -r git branch -d

I'm splitting up the command onto separate lines for readability. The -r will only run the git command if there is output.

I've noticed this doesn't always delete all old branches, but it can help. For example, perhaps you had a branch that was a work in progress, or a spike, with changes that aren't merged - this won't be picked up.

You can do one more trick to detect old branches that have no upstream branch anymore:

$ git branch -vv

This will list branches without an upstream branch with gone. Those can generally be deleted too.

Wednesday, May 16, 2018

git-run for managing many repositories

Some may make use of a monorepo, for example over there at Google, however for those of us that don't and have to deal with multiple repositories, possibly unrelated ones across various projects, then a great tool to have in your toolbelt is git-run, or just gr.

It can keep track of where your repositories are located, and can also be used to tag the various repositories. You can then run a git command across all of these repositories (or based on a tag).

To set it up:

$ npm install -g git-run
$ git tag discover PATH

One way I have been using that is to create a tag per project, so across a project which may have many repositories, I can see my git status across all of those by executing:

$ gr @project status

It can also run normal shell commands as well

$ gr @project cat package.json


One thing I realised it didn't do in my git bash shell, was run my aliases that I tend to use (e.g. I have a complicated bash alias for a git log command). But luckily someone else has already got an open pull request to fix this (at least for bash), which suited my needs. Yay for open source!

Try it out and let me know what you think :)

Update: I also recently discovered when trying it on a new computer - make sure you don't forget to add tags to the repositories it discovers during gr tag discover, because it won't save its configuration and you'll get an error.

Thursday, November 30, 2017

bash startup files and aliases

Today I hosted a Beer and Tech session at Entelect on Automating Database Deployments (with Flyway).

During the session, I was using the Bash terminal to edit files or run Flyway, and interacting with git quite a bit - as we all know, using git on the command line is the best way to do so ;D

After the session, a colleague asked me how I created shortcuts for certain commands, and I explained to him how Bash supports aliases and that these can be loaded by a startup file. There are various startup files, but I tend to use ~/.bash_profile and not ~/.bashrc. Here are some of the shortcuts I currently have setup:

alias cls='clear'
alias l='ls -Al'
alias ..='cd ..'

alias gf='git fetch --prune'
alias gl='git log --name-status --abbrev-commit'
alias grh='git reset --hard'
alias gs='git status'

And so on. If you modify the file, you have to re-load these into the current session, or restart a new terminal session. To reload the file, you can just do the following:

$ . ~/.bash_profile

Hope this helped someone :)

Thursday, July 20, 2017

git - exclude files in a diff

In some scenarios you might have a build server that doesn't have access to the Internet, in which case if you're working with something like npm, you might need to commit your node_modules folder to the git repository to get stuff to work.

Of course, it might be better to setup your own artifact repository such as Nexus on an internal network for these dependencies, BUT, I didn't have that! :D The node_modules are included as part of a commit.

Having to do a code review of a pull request can be tricky because of this, due to more than 10,000 files being in the commit, of which only 40 are actual source files of interest. We're using TFS 2017 which has a nice web interface for doing this, however it only supports showing 1000 files at a time.

So back to the best way to use git: the command line :)

To exclude the node_modules from the diff was as simple as doing the below:

$ git diff master develop -- . ':!node_modules'

Of course, I used difftool because I prefer WinMerge but it works the same.

Thursday, February 14, 2013

Git tip - diff of only modified files

I was wondering how to see a diff of only those items that were modified in my Git workspace, and thanks to Stackoverflow, you can use the --diff-filter parameter:

git diff --diff-filter=M
git dt --diff-filter=M

The second one is if you use a visual diff tool. I prefer WinMerge, might do a post on my setup at some point, it's pretty simple once you know how.