Skip to content

Git Tips and Tricks

Git is an amazing tool that does a lot of incredible things. It's also a tool that is easy to screw up. Here is a list of tips I've found useful for when working with git.

Git Up

So when you run git pull chances are what you actually want to do is rebase over them. You also are probably working in a "dirty workspace" which means you want to pull the stuff ahead of you and rebase over it but until you are ready to commit, you don't want your work to be reflected. (I'm saying all this like its a certainty, I'm sure for a lot of people it's not. However I work in an extremely busy repo all day).

We're gonna set a new git alias for us that will do what we actually want to do with git.
git config --global alias.up 'pull --rebase --autostash'

Autostash basically makes a temporary stash before you run the rest of the rebase and then applies it back after the operation ends. This lets you work out of a dirty directory. Make sure you sanity check your changes though since the final stash application has the potential to do some weird things.

Approving Stuff As You Git Add

Been working for awhile and ready to add stuff to a commit but maybe don't remember all the changes you've made? Not a problem.

git add -p lets you step through your changes one by one and only add the ones to this commit you need. I use this as a final sanity check to ensure some fix I didn't mean to go out with this deploy doesn't sneak out.

Go Back In Commit History

git reflog will show you a list of everything done across all branches along with the index HEAD@{index}

git reset HEAD@{index} will let you fix it

Commited to the wrong branch

Go to the branch you want: git checkout right-branch

Grab your commit: git cherry-pick SHA of commit (get this from git log) or if its the last commit on a branch just the name of the branch

If needed, delete the commit from the wrong branch: git checkout wrong-branch then git reset HEAD~ --hard to knock out the last commit

You can pass an argument to HEAD to undo multiple commits if needed, knucklehead: git reset HEAD~3 to undo three

Squash Commits Down

First just do a soft reset passing HEAD the argument for how many commits you want to go back: git reset --soft HEAD~3

Then take your previous commit messages and make them your new unified commit: git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"

Fix Messed Up Commit Message

git commit --amend and then just follow the prompts


MegaCLI Cheat Sheet

Everyone who has worked with specific vendors servers for awhile has likely encountered megacli. This is the comman line tool to manage your RAID controller and disks. Since often I'll only end up doing this every six months or so, I usually forget the syntax and decided to write it down here.

Install megacli

sudo apt-get install megacli

Common Parameters

Controller Syntax: -aN

This is telling MegaCLI the adapter ID. Don't use the ALL flag, always use this because you'll get in a bad habit and forget which boxes have multiple controllers

Physical drive: -PhysDrv [E:S]

I hate this format. E is the enclosure drive ID where the drive is and S is the slot number starting at 0. You get this info with the megacli -EncInfo -aALL

Virtual drive: -Lx

Used for specifying the virtual drive (where x is a number starting with zero or the string all).

Information Commands

Get Controller Information:

  • megacli -AdpAllInfo -aALL
  • megacli -CfgDsply -aALL
  • megacli -adpeventlog -getevents -f lsi-events.log -a0 -nolog

Common Operations

Replace drive:

  • Set the drive offline with megacli -PDOffline -PhysDrv[E:S] -aN
  • Mark the drive as missing: megacli -PDMarkMissing -PhysDrv [E:S] -aN
  • Get the drive ready to yank out: megacli -PDPrpRmv -PhysDrv [E:S] -aN
  • Change/replace the drive: megacli -PdReplaceMissing -PhysDrv[E:S] -ArrayN -rowN -aN
  • Start the drive: megacli -PDRbld -Start -PhysDrv [E:S] -aN

Fix Drive marked as "foreign" when inserted:

This happens when you steal a drive from a existing RAID to put in your more critical array. I know, you've never done anything so evil. Anyway when you need to here's how you do it.

  • Flip to good: megacli -PDMakeGood -PhysDrv [E:S] -aALL
  • Clear the foreign (that feels offensive but we're gonna move on): megacli -CfgForeign -Clear -aALL

Shut the stupid alarm off for good

  • megacli -AdpSetProp AlarmDsbl -aALL