Git 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.
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
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 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
git difftool $start_commit..$end_commit
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:
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
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.
Using completion scripts, you can quickly create the commands for
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
If you want to avoid committing files like
.DS_Store or Vim
swp files, you can set up a global
Create the file:
git config --global core.excludesFile ~/.gitignore
Or manually add the following to your
[core] excludesFile = ~/.gitignore
You can create a list of the things you want Git to ignore. To learn more, visit the gitignore documentation.
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
[fetch] prune = true
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
@ is the same as
HEAD. Using it during a rebase is a lifesaver:
git rebase -i @~2
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
If you want to elevate Git with more commands, try out the git-extras plugin, which includes
git info (show information about the repository) and
git effort (number of commits per file).