10 useful Git tips to improve your workflow
Tags: git, beginners, productivity
Published: 2021-12-11 12:20:02

[Git](https://git-scm.com/) is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. 

Whether you’re just getting started with Git, or you know your way around a command line, it’s always nice to brush up on your skills. Below I would like to share 10 useful Git tips to improve your Git-based workflow.

---

##1. Git aliases 🏷
Create your own aliases for common commands to save you some time in the terminal.
```
git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.br branch
```

Instead of typing `git checkout master`, you only need to type `git co master`.

You could also edit these commands or add more by modifying the `~/.gitconfig` file directly:

```
[alias]
    co = checkout
    ci = commit
    br = branch
```

##2. Compare commits ⚖
A simple way to compare the differences between commits or versions of the same file is to use the `git diff` command.

If you want to compare the same file between different commits, you run the following:

```
git diff $start_commit..$end_commit -- path/to/file
```

If you want to compare the changes between two commits:
```
git diff $start_commit..$end_commit
```

These commands will open the diff view inside the terminal, but if you prefer to use a more visual tool to compare your diffs, you can use `git difftool`. [Meld](https://meldmerge.org/) is a useful viewer/editor to visually compare diffs.

To configure Meld:
```
git config --global diff.tool git-meld
```

To start viewing the diffs:
```
git difftool $start_commit..$end_commit -- path/to/file
```
Or
```
git difftool $start_commit..$end_commit
```

##3. Stashing uncommitted changes 🔖
If you’re ever working on a feature and need to do an emergency fix on the project, you could run into a problem.😰 You don’t want to commit an unfinished feature, and you also don’t want to lose current changes. The solution is to temporarily remove these changes with the Git stash command:
```
git stash
```

The git stash command hides changes, giving you a clean working directory and the ability to switch to a new branch to make updates, without having to commit a meaningless snapshot in order to save the current state.

Once you’re done working on a fix and want to revisit your previous changes, you can run:
```
git stash pop
```

And your changes will be recovered. 🎉

If you no longer need those changes and want to clear the stash stack, you can do so with:

```
git stash drop
```

##4. Pull frequently 📥
In order to avoid major conflicts, you should frequently pull the changes from the master branch to your branch to resolve any conflicts as soon as possible and to make merging your branch to master easier.

##5. Autocomplete commands 🤖
Using [completion scripts](https://github.com/git/git/tree/master/contrib/completion), you can quickly create the commands for `bash`, `tcsh` and `zsh`. If you want to type `git pull`, you can type just the first letter with `git p` followed by `Tab` will show the following:

```
pack-objects   -- create packed archive of objects
pack-redundant -- find redundant pack files
pack-refs      -- pack heads and tags for efficient repository access
parse-remote   -- routines to help parsing remote repository access parameters
patch-id       -- compute unique ID for a patch
prune          -- prune all unreachable objects from the object database
prune-packed   -- remove extra objects that are already in pack files
pull           -- fetch from and merge with another repository or local branch
push           -- update remote refs along with associated objects
```

To show all available commands, type `git` in your terminal followed by `Tab` + `Tab`.

##6. Set a global `.gitignore` 🚫
If you want to avoid committing files like `.DS_Store` or Vim `swp` files, you can set up a global `.gitignore` file.

Create the file:
```
touch ~/.gitignore
```
Then run:
```
git config --global core.excludesFile ~/.gitignore
```

Or manually add the following to your `~/.gitconfig`:
```
[core]
  excludesFile = ~/.gitignore
```

You can create a list of the things you want Git to ignore. To learn more, visit the [gitignore documentation](https://git-scm.com/docs/gitignore).

##7. Delete local branches that have been removed from remote on fetch/pull 🗑

You likely have stale branches in your local repository that no longer exist in the remote one. To delete them in each fetch/pull, run:

```
git config --global fetch.prune true
```
Or manually add the following to your `~/.gitconfig`:
```
[fetch]
  prune = true
```

##8. Use Git blame more efficiently 🕵️‍♂️
Git blame is a handy way to discover who changed a line in a file. Depending on what you want to show, you can pass different flags:

```
git blame -w  # ignores white space

git blame -M  # ignores moving text

git blame -C  # ignores moving text into other files
```

##9. An alias of `HEAD` 👀
`@` is the same as `HEAD`. Using it during a rebase is a lifesaver:
```
git rebase -i @~2
```

##10. Resetting files ↩
You’re modifying your code when you suddenly realize that the changes you made are not great, and you’d like to reset them. Rather than clicking undo on everything you edited, you can reset your files to the HEAD of the branch
```
git reset --hard HEAD
```
Or if you want to reset a single file:
```
git checkout HEAD -- path/to/file
```
Now, if you already committed your changes, but still want to revert back, you can use:
```
git reset --soft HEAD~1
```

## Extras: ✨
If you want to elevate Git with more commands, try out the [git-extras plugin](https://github.com/tj/git-extras), which includes `git info` (show information about the repository) and `git effort` (number of commits per file).