This post will not be about Ruby, but about another tool we use on a daily basis!
It also doesn’t have a “story”, it is a collection of random Git tips that helped me in the recent past that I do not want to forget about π
And just as a note, this is far from a complete list, so if you think this list needs something, just send me as a comment and I’ll update the post, thank you in advance for any contribution.
Make ‘git branch’ list branches in last committed order
Add this snippet to your ~/.gitconfig
[branch] sort=-committerdate # DESC #sort=committerdate # ASC
Find commits in the entire tree that have a specific file in it, and the branches that contain it
This one and the next helped me to find the branch I needed to deploy just knowing the name of a file included in that deploy, first you search for the commits that have a file:
$ git log --all -- <filename>
Then you take one of the hashes from the previous command and search for branches that have that commit in it:
$ git branch -a --contains <hash>
List the branches you have or haven’t merged in another branch
$ git checkout target_branch $ git branch -a --no-merge # list what was not merged to target_branch $ git branch -a --merged # list what was merged to target_branch
I’ve already used this combination to check what branches were merged to QA and not merged to production for example, just pipe the results to files and do a diff π
A global .gitignore
I usually find really annoying to configure my editor ignores in every project, so I’ve created a global git ignore, you can add anything to it, for example, I’ve created my ~/.gitignore with this content:
*.log .idea/ *.iml node_modules yarn-debug.log* .yarn-integrity .vscode/* *.sublime-workspace *.sublime-project
But just creating this file wont do the trick as I found out after adding some of those files to repositories by mistake, you need to tell git to use that file with the following command:
$ git config --global core.excludesFile ~/.gitignore
Delete local branches that do not exists in the remote anymore
I create a lot of branches, then the branches are merged to master and deleted from the remote repository, but that usually leaves me with a pretty large branch list locally, to delete local branches that do not exists in the remote anymore just run this quick command:
$ git remote prune origin
Export a branch with history to a file.
$ git bundle create [file] [branch-name]
Import from a bundle
$ git clone repo.bundle [repo-dir] -b [branch-name]
Untrack files without deleting
Lots of times I add a file to the next commit by mistake, and this command has helped me a lot!
$git rm --cached [file_path]
Checkout one file from a specific branch
Many times I want a file from a specific branch (works for any tree-ish) for many reasons, and it is really easy to checkout one file from a branch, just use this command:
$ git checkout master -- [filename]
Of course, replace master with any branch name you need there.