Git CLI tips and tricks to be more productive
Tags: productivity, git, tips, tutorial
Published: 2020-09-08 09:34:45

If you are a software developer you must be aware of the most popular version control system used `git`. I use git CLI for my day-to-day developer tasks, few prefer GUI clients such as `Sourcetree` over CLI.

In this post, I'm going to explain to you the useful commands that I've been using for years and have benefited a lot to be productive.

Let's get started,

## Checkout to last checked out branch

Let us say you have created a `fix-payment-bill-issue` branch from the `master` branch to fix a bug and want to switch to the last branch you were in. This will be useful when you don't remember the branch name that you have checked out last time.
```
$ git checkout -
```

### 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 the branch, make sure that the file is committed in another branch before copying it.
  ```
  $ git checkout <target-branch> -- <complete/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.
  ```
  $ git commit -a --amend -C HEAD
  ```

### Remove the branches that are deleted from remote in the local copy

   ```
   $ git remote prune origin

   $ git fetch -p
   ```

### Aliases

git provides a feature called `aliases`, which helps you to define aliases for the list of commands you use more frequently and help you get the work done faster instead of typing out the whole commands in the command line.

- 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 the changes from the remote, it removes any remote-tracking references that no longer exist on the remote.
  ```
  $ git config --global alias.f=fetch -p
  ```
  
- List all the modified files in the current branch, it will give you a holistic picture of which files are committed before pushing to the remote repo.
  ```
  $ git config --global alias.list-files=show --pretty="" --name-only HEAD
  ```
  
- Sort all the branches that you have created in descending order with author name and other details. Refer 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'|'"
  ```
> 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. Prefer using the single cloned copy, create as many features, or bug fix branches in the same local copy so that it is easy to look back on history to see which fix/feature went when.


- Other useful aliases
  ```
  # pull changes from master
  $ git config --global alias.pom=pull origin master/main

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

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

  $ git config --global alias.cl=checkout -
  ```
  
-  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 master and merges with the master, then checkout to the last branch point you were in, and finally releases the master changes with the current branch.

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

Hope this will help you learn a few git tips and tricks and increases your productivity over time.

Please leave a comment if you want me to cover the basics of git or any other topic related to git. Follow me for more technical content related to JS, React, NodeJS, and other libraries or tools.

Happy coding.