greckout - a bash script to grep and checkout your git branches

greckout query

This will list all branches matching your query as input options for git checkout

  greckout ar
  
  1) ar/cache-api-keys-1098
  2) ar/add-categories-object-to-tv-show-1382
  3) ...

geewax.org | Agile git Workflow

When we started using git to manage our source code at work, we actually jumped in a little bit too fast. It seems like there is a lot of writing about how you can do lots of really neat things with git, but no real guide about one particular way of using git for your project. This post is going to describe how we use git day to day on a reasonably large “agile-style” project.

Git: See all unpushed commits or commits that are not in another branch

If you need to find out which of your local commits are not on the remote server do this:

git cherry -v

The -v option prints out the commit messages. Without it you will see only the SHA1 codes.

You may also compare against another (upstream) branch like that:

git cherry -v origin/somebranch

This tool is especially useful when you have a ton of commits after a merge and want to know the commit differences between branches.

Typical .gitignore

log/*
tmp/*
storage/*
db/*.sqlite3
db/schema.rb
db/structure.sql
public/system
.project
.idea/
public/javascripts/all*
public/stylesheets/all*
public/stylesheets/*.css
config/database.yml
*~
*#*
.#*
.DS_Store
webrat-*.html
capybara-*.html
rerun.txt
coverage.data
coverage/*
dump_for_download.dump
.~lock.*
.*.swp
C:\\nppdf32Log\\debuglog.txt

How to install a current version of git to your Ubuntu machine

As described by the linked Stackoverflow answer, run these commands:

sudo add-apt-repository ppa:git-core/ppa -y
sudo apt-get update
sudo apt-get install git
git --version

This will get you Git 2.6.4 (as of Dec 2015).

Troubleshooting

If you don't have add-apt-repository yet, install it with:

sudo apt-get install python-software-properties software-properties-common

Git: See more context in a diff

Using the -U parameter you can change how many lines are shown above and below a changed section.

E.g. git diff -U10 will show 10 lines above and below.

When RubyMine doesn't show you Git blames / annotate gutter not available

  • You probably haven't configured version control for your project.
  • Go to Project Settings / Version Control and set the director <Project> to Git

Git: List remote branches

Sometimes you may need to figure out what branches exist on a remote repository so you can pull them down and check them out, merge them into your local branches, etc. You can see the remote branches by saying

git branch -r

Or, if you want to see both local and remote branches, you can say

git branch -a

Too Smart for Git

The problem isn't that Git is to hard, it's that smart developers are impatient and have exactly zero tolerance for unexpected behavior in their tools. While Git is the trendy thing right now, perhaps some day you will come across a grizzled developer who is using SVN, and when you ask him why, his answer won't make sense, because it's a Zen thing.

git: How to forget a recorded resolution

If you recorded a bad fix for a conflict, you can tell git to forget that bad resolution:

git rerere forget your_file.rb

Afterwards, the badly resolved file will still be in your working directory. To get it back with confict markers, say:

git checkout -m your_file.rb

Resolve the conflict again, properly now. ;)

Visualized introduction how git works

Quick introduction to git internals for people who are not scared by words like Directed Acyclic Graph.


The linked page offers a simple yet concise explanation of how git is organized internally ('a directed acyclic graph with post-it notes'). Each feature is illustrated by a simple diagram, so you get a sound understanding of how each command affects git's structure.

Git diff a file with another revision (or branch)

git diff commit_hash -- path/to/file

Provide any commit hashes or branch names like "master" for commit_hash.

An interactive Git shell

Git commands tend to come in groups. Avoid typing git over and over and over by running them in a dedicated git shell.

You might want to run git config --global help.autocorrect true before using gitsh. This will silently swallow a muscle-memory "git" prefix to your commands inside the git shell.

Git: Add all changes

A nice way to stage absolutely all changes (edits, additions, deletions):

git add --all

Git: Show current branch name only

While git branch will show you all branches and highlight the current one with an asterisk, it can be too cumbersome when working with lots of branches.

To show only the branch you are currently on, use:

git rev-parse --abbrev-ref HEAD

Trouble changing filename casing in git

On case-insensitive file systems like Mac's HFS+ (per default – you may change this, but it won't work for all programs), git won't recognize when you change the casing of files in your repository.

Workaround

git mv -f oldfile.name Oldfile.name

Auto-squashing Git Commits

git command line options for automating common rebasing tasks, like adding a fix to a commit that was already rebased into the history.

How to undo a 'git reset --hard'

So you erased a whole day's work? There is hope! The linked article tells how to recover from an accidental git reset --hard.

On a Mac

You will need to install GNU find utils as described in this SO post.

Git Landscaping

I recently worked on a project with 60+ old feature branches. Most of them had been merged into master and were subsequently abandoned. We decided we wanted to clean up a bit, and git made that easy.

Git Cheatsheet

Make sure you understand differences between git's areas (such as stash, workspace, upstream, etc.) and what commands affect which areas.

Git: Issues with Gemfile.lock

When there's a Gemfile.lock in your working directory that you cannot remove by either checkout, reset [--hard], stash, probably Rails' Spring is the culprit and not Bundler itself.

Fix

spring stop

The author of the linked Stackoverflow post supposes Spring re-writes the Gemfile.lock on change to ensure all Spring processes are using the same gem versions. Meh.

Git: In an (interactive) rebase, find out which commit you are currently working on (until version < 1.7.9.5)

When you are using git rebase and are currently editing a commit (due to a conflict, for example), you may want to know the current commit. [1]\
Luckily, there is lots of useful stuff in the .git directory.

Commit hash that you are currently applying

cat .git/rebase-merge/stopped-sha

Useful if you want to inspect the original commit for its changes, for example like:

git show `cat .git/rebase-merge/stopped-sha`

Current commit's message

cat .git/rebase-merge/message

In case you forgot what the changes are suppo...

Bookmarklet to facilitate generating new git branches for PivotalTracker Stories

This bookmarklet grabs a PivotalTracker story title, transforms it into a valid git branch name and automatically prepends your initials and an optional abbreviation (for better tab completion). It will output the following formats:

If you cancel the first dialog or confirm it without entering text:

git checkout -b kw/178298638-card-320-state-machines

If you enter an abbreviation (e.g. stm in this case):

git checkout -b kw/stm/178298638-card-320-state-machines

How to set it up:

  • in the attached file replace `YOUR_INITI...

Git: Change the text editor used to enter commit messages

In your ~/.gitconfig:

[core]
  editor=nano