--- title: "8 Tips to help you work better with Git" date: 2015-02-19 image_title: '/images/unsplash/leaves.jpeg' categories: open source author: Patricio Cano author_twitter: suprnova32 --- Git is a very powerful version control system. It can be a little bit daunting to try to learn everything around it, so most people just use the basic commands. We want to give you here some help with some tips you may or may not have heard. Either way, these tips can make your workflow a little easier. ## Git aliases One of the best ways to ease your daily workflow with Git is to create aliases for common commands you use every day. This can save you some time in the terminal. You can use the following commands to create aliases for the most used Git commands, `checkout`, `commit` and `branch`. ``` git config --global alias.co checkout git config --global alias.ci commit git config --global alias.br branch ``` This way, instead of typing `git checkout master` you only need to type `git co master`. You could also edit them or add more by modifying the `~/.gitconfig` file directly: ``` [alias] co = checkout ci = commit br = branch ``` ## Stashing uncommitted changes Let’s say we are working on a new feature, but there is an emergency and we need to fix to our project immediately. We don’t want to commit an unfinished feature, and we also don’t want to lose our current changes. The solution is to temporarily remove these changes with the git stash command: ``` $ git stash ``` The git stash command hides these changes, giving us a clean working directory. We’re now able to switch to a new branch to make our important updates, without having to commit a meaningless snapshot just to save our current state. Once you are done working on the fix and want to show your previous changes again, all you need to is 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 ``` ## Compare commits from the command line An easy and quick way to compare the differences between commits, or versions of the same file is to use the command line. For this you can use the `git diff` command. If you want to compare the same file between different commits, you do the following: ``` $ git diff $start_commit..$end_commit -- path/to/file ``` And 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`. A really great diff viewer/editor is Meld. To configure Meld: ``` $ git config --global diff.tool git-meld ``` Now to start viewing the diffs: ``` $ git difftool $start_commit..$end_commit -- path/to/file # or $ git difftool $start_commit..$end_commit ``` ## Resetting files Sometimes when you start modifying your code, you realize that the changes you did are not that good and would like to reset your changes. Instead of 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 ``` ## Use Git blame more efficiently Git blame is a great tool for finding out who changed a line in a file, but there are ways you can use it more efficiently. You can pass different flags, depending on what you want to show. ``` $ git blame -w # ignores white space $ git blame -M # ignores moving text $ git blame -C # ignores moving text into other files ```