Git tips to be more productive

·

5 min read

Image for post

I always loved CLI for my day-to-day developer tasks, I use it for rebasing, committing and resolving merge conflicts, and so on. Few prefer GUI clients such as Sourcetree over CLI and that is just an individual preference, being a developer using a CLI makes you look cool and awesome 😎

In this post, I’m going to explain to you the useful commands that I have been using for years and have helped me to be more productive, hope this post will help you to be a little more productive than before.

Checkout to last checked out branch

Let us say you have created a fix-payment-bill-issue branch from the master to fix a bug and want to switch to the last branch you were in, use the following command.

$ git checkout -

This will be useful when you don't remember the branch name that you checked out last time.

Check whether the current branch is merged with the targeted branch

This will be useful to know whether your changes were merged with the targeted branch.

$ git branch --merged <targeted-branch>

Copy the files from another branch without switching the branch

Copy a file from another branch without switching branch, make sure the file is committed in another branch.

$ git checkout <target-branch> -- <full/path/to/file.js>

Search for a particular text in the commit message

Search git log with a particular text string, it lists all the commits matching the string

$ git log -S'<search-string>'

Add the modified and uncommitted files to the previous commit

Add the modified and uncommitted files to the previous commit without changing the commit message, new files should be added to the staging before running this command, use git add .

$ git commit -a --amend -C HEAD

Skip the hooks from executing the commit

Let's say you want to commit and publish the changes for quick testing or some feedback from your teammates, but you have configured hooks to check for linting or run unit tests before committing the changes and there are lots of issues/warnings in your code (lint errors, warnings, console logs, etc.,), but you still want to push, here is a way to bypass that gate, use the option --no-verify or the shorthand -n when committing your changes as,

$ git commit -n

Aliases: Type less in the CLI

Who likes to type more characters, when we can use git aliases, git does provide a feature called aliases to be configured to use in git projects across the file system or on a single project.

Here are the aliases I used more frequently.

Print the last 15 commits with less info

$ git config  --global alias.l15=log --oneline -15

Use --local option instead of --global to apply these aliases to the individual repo.

Fetch with pruning, removes any remote-tracking references that no longer exist on the remote.

$ git config  --global alias.f=fetch -p

List all the files that you have made the changes to and committed to the branch, it will give you a holistic picture of which files are committed part of the commit before pushing to remote.

$ git config  --global alias.list-files=show  --pretty=""  --name-only HEAD

Sort all the branches that you have created in descending order with the author's name and other details. Refer to the documentation to customize the fields you want to display.

$ git config --global alias.sort-branch=!"git for-each-ref --sort=-committerdate refs/heads --format='%(HEAD)%(refname:short)|%(committerdate:relative)|%(subject)'|column -ts'|'"</span>

If you delete the cloned copy after a feature/bug is completed and clone the fresh copy each time you work on a new feature/bug, the above alias is of no use. I prefer using the single cloned copy, creating as many features or bug fix branches in the local copy so that it is easy to look back on history to see which fix/feature went when.

Other aliases that I use more frequently

# pull changes from master 
$ git config  --global alias.pom=pull origin master

# Rebase with master 
$ git config  --global alias.rbm=rebase master

# To get the list of all the global configs on this the machine 
# Change it to --local to see a local configuration 
$ git config  --global alias.config-list=config  --global --list

Use the created alias as follows, git command before the alias name is mandatory

$ git f

If you are on macOS or Linux distribution, you can combine more than one command with short-circuiting to execute the commands sequentially.

$ git cm && git f && git pom && git cl && git rbm

Above command checks out to the master branch from the current branch, then fetches the remote changes, then pulls the changes from the master and merges with the master, then checkout to the last branch you were in, and finally rebases the master changes with the current branch.

See how easy it is to achieve more with less typing.

I recommend you to type the full commands until you are familiar with them so that you won’t forget them because you have aliases defined.

One funny thing that I’ve noticed with the aliases over the years was when my colleagues asked me to look at some code that they are working on their machine and I try to use an alias that I've created in my machine but it never worked because they haven’t added any aliases with the same name like the one in my machine, later I realized that it is not my machine 😀

Hope the above tips will be useful to you one day, please share and follow me for more on full stack development posts and tips.