For most of the my projects I use Git as a distributed version-control system to track changes in the source code for both my personal and work related projects. Even though Git is around for several years now (since 2005), a lot of people are still relatively new to Git. This post goes through some of the Git tips and tricks I learned by using Git everyday, and is geared towards Git beginners.
- Git help
- Git checkout
- Git configuration
- Visualizing branch topology
git help you can display a synopsis of the help information about Git, this includes a list of commonly used Git commands. Whenever the
-a option is used, all the available commands are shown. If you want to learn more about a specific command your can simply run
git help COMMAND.
Besides the displaying the help information using
git help, Git includes some very useful guides with the
git help --guides command:
The common Git guides are: attributes Defining attributes per path cli Git command-line interface and conventions core-tutorial A Git core tutorial for developers cvs-migration Git for CVS users diffcore Tweaking diff output everyday A useful minimum set of commands for Everyday Git glossary A Git Glossary hooks Hooks used by Git ignore Specifies intentionally untracked files to ignore modules Defining submodule properties namespaces Git namespaces repository-layout Git Repository Layout revisions Specifying revisions and ranges for Git tutorial A tutorial introduction to Git tutorial-2 A tutorial introduction to Git: part two workflows An overview of recommended workflows with Git
git help everyday to learn the most common Git commands, or execute
git help workflows to get an overview of the recommend Git workflows.
Whenever you switch a branch in Git you will you use the git-checkout command. But did you known you can switch more than just branches?
Checking out a file from another branch
The git-checkout command allows the user to checkout files from another branch. For example, while being on the
master branch you can checkout just the
README.md file from the
git checkout master git checkout develop -- README.md
The pattern is the following:
git checkout <branch_name> -- <paths>
If you ever followed a Git tutorial for beginners, you probably came across the
git config --global user.name "John Doe" command. You can get an set a lot more repository and global Git options using git-config. The Git configuration gets stored in the
~/.gitconfig file. For repository specific Git configuration, you can create a
.gitconfig file in the base of your repository.
git config --global user.name "John Doe" result in the following
[user] name = John Doe
So you can also edit the
~/.gitconfig file directly and add it to your personal dotfiles.
Visualizing branch topology
Some people favor the tree like-view that comes with most graphical tools when displaying the Git log. It's also possible to do this in the terminal using the following command:
git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(auto)%d%C(reset)'
For the SaltStack project this looks like this:
The only downside is that the command is hard to remember. Instead of copying and pasting the command, you can also create a Git alias using the following command:
git config --global alias.graph "log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(auto)%d%C(reset)'"
Or place it directly in you
[alias] graph = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(auto)%d%C(reset)'
You can now simply run
git graph to get the Git log in a tree-like style!