Skip to content

Rodrigo Urubatan – About Code

Helping ruby developers to use the best tools for each job so they can solve hard problems, with less bugs and have more free time.

Menu
  • Home
  • My last presentations
  • About
  • Privacy Policy
Menu

Random collection of Git Tips and Tricks

Posted on 2019-06-04 by Rodrigo Urubatan

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.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent posts

  • I see Dead Jobs everywhere (sidekiq DeadSet)
  • Quick tips that help: rails notes
  • Ruby 3.2.0 released with WASI, YJIT, RegExp improvements, …
  • Rubyconf Thailand quick summary and slides
  • Testing download contents with Cucumber+Capybara and chromedriver

Comments

  1. When Kubernetes is not the right choice? | Rodrigo Urubatan - About Code on Rails from “zero” to kubernetes – first pod
  2. When Kubernetes is not the right choice? | Rodrigo Urubatan - About Code on How to use docker to have an uniform development environment for your rails project
  3. Rails from "zero" to kubernetes – ingress reverse proxy | Rodrigo Urubatan - About Code on Rails from “zero” to kubernetes – a service to access your pod
  4. Rails from "zero" to kubernetes – horizontal autoscaling | Rodrigo Urubatan - About Code on Rails from “zero” to kubernetes – a service to access your pod
  5. Jeronimo on 6 Lessons From CrossFit That will help your developer career (Or any other career in the matter of fact)

Arquives

  • February 2023
  • January 2023
  • December 2022
  • June 2021
  • March 2020
  • January 2020
  • July 2019
  • June 2019
  • May 2019
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • February 2018
  • January 2018
  • November 2017
  • August 2015
  • August 2014
  • July 2014
  • August 2007

Categories

  • articles
  • cfp
  • firebase
  • gems
  • git
  • opinion
  • presentations
  • projects
  • rails6
  • ruby
  • Sem categoria
  • server-api
  • Uncategorized
© 2023 Rodrigo Urubatan – About Code | Powered by Minimalist Blog WordPress Theme