5.4. Git#

5.4.1. Aliasing git commands#

If you are anything like me, you would be lazy to even type three-word git commands.

In that case, by using aliasing in bash or any Linux terminal, you can significantly shorten your git commands. Here are a few I use daily:

5.4.2. Undoing git history when you mess up#

While debugging, you often realize that the error is caused by a mistake made long ago in the project history. Instead of going back to that commit, you have the option of uncommitting everything since that point.

Two popular ways of doing this:

  1. Uncommitting all changes since that commit and add them to the working directory (soft reset)

  2. Uncommitting all changes and deleting project history since that point (hard reset, difficult to recover)

Both can be done with “git reset”. Note that you can use commit hash as well instead of HEAD~n syntax.

5.4.3. Removing large files from git history#

Accidentally committing a large CSV file to Git is practically a rite of passage for data scientists. We have all done it at least once in our life.

That feeling of horror as you watch “git push” never actually pushing anything and you saying, “oh, not again!”. That isn’t fun.

For situations like this, I have a StackOverflow thread bookmarked which links directly to the trick below. Running it will eradicate any record of a file or folder from Git history - across all commits and branches.

Be aware that it will take longer if your repo is large.

5.4.4. Reviewing commit history without losing changes#

While working on a new feature, sometimes you want to go for a quick peek into project history.

Unfortunately, Git doesn’t allow moving the HEAD without forcing you to commit the changes in the working directory. You can get around this pesky problem with a simple stashing trick: