If you use Git on a daily basis, you know that this tool is full of commands and different configuration options. I suggest here to list some tips that I use.
$ git config --global user.name "Darth Vader" $ git config --global user.email "firstname.lastname@example.org"
I voluntarily start simple but it is a manipulation that happens to me quite often, my computer being used to manage several projects with different identities.
So if you have different information for a project, you can override this declaration at the level of your project:
$ cd ~/dev/my_project $ git config user.name "Luke Skywalker" $ git config user.email "email@example.com"
I recommend that you set up a global
.gitignore file for your development environment in order to ignore the files related to your way of working (IDE used for example).
This avoids finding all the IDEs in the
.gitignore files of your projects.
$ vi ~/.gitignore
Example with PHPStorm / Intellij directory
$ git config --global core.excludesfile ~/.gitignore
Click the following link to find a collection of
gitignore file templates: https://github.com/github/gitignore
If you don't want to use
vi, ask git to use another editor for your commits !
# With nano $ git config --global core.editor nano # You can also choose a graphical editor # Visual Studio Code $ git config --global core.editor "code --wait" # Sublime text $ git config --global core.editor "subl -n -w" # Atom $ git config --global core.editor "atom --wait"
If the format of the
git log command does not suit you, you can change it or define a custom one.
There are several predefined formats:
# One line display by default $ git config --global format.pretty oneline # You can also define your own log display format $ git config --global format.pretty "format:%h%x09%an" # And save it $ git config --global pretty.my-custom-log-format "format:%h%x09%an" # Which allows either to use it on demand $ git log --pretty=my-custom-log-format # Or define it as the default configuration $ git config --global format.pretty my-custom-log-format
The list of formats and placeholders is available : https://git-scm.com/docs/pretty-formats
You probably know the
--amend option in order to modify the last commit. But do you know the option
--fixup and the
autosquash to modify any previous commit.
Warning : the
autosquash will modify the references of the commits in your branch. Not recommended if you are not comfortable with rebases and what it means.
You can do it serenely if you don't haven't pushed anything on remote yet.
Take the example where you are going to add a README file to your project.
List of commits on your branch where you are writing the file
$ git log
b58faee269c Add contribution in README 18a11d43eb2 Add deployment in README 8385fc0cd1a Initial commit
And there you see an error in the commit to add the deployment part (spelling error for example) (ref commit: 18a11d43eb2).
We fix the error and commit with the
$ git commit --fixup 18a11d43eb2 94ae1b02f4a fixup! Add deployment in README
git log now looks like:
$ git log 94ae1b02f4a fixup! Add deployment in README b58faee269c Add contribution in README 18a11d43eb2 Add deployment in README 8385fc0cd1a Initial commit
We can then merge the original commit with the fixup commit
$ git rebase -i --autosquash 8385fc0cd1a
The fixup commit was squashed in the deployment section commit. We can see here the modification of the commits references.
$ git log fc5c36d4183 Add contribution in README 0a94be69d2c Add deployment in README 8385fc0cd1a Initial commit
Git lets you pre-fill commit messages from a template.
$ vi ~/.gitmessage
Short Title IssueNumber Why :
$ git config --global commit.template "~/.gitmessage"
Since version 2.23, a new command is available to switch branches.
Usually we use
$ git checkout my_branch Switched to branch 'my_branch'
We can now use the
$ git switch my_branch Switched to branch 'my_branch'
And if we want to create a new branch and
switch on it
$ git switch -c my_new_branch # equivalent to git checkout -b my_new_branch Switched to a new branch 'my_new_branch'
If you are a bit lazy like me, you can go back to the previous branch using the
git switch - command
(master) $ git switch my_branch Switched to branch 'my_branch' (my_branch) $ git switch - Switched to branch 'master'
It also works by doing
git checkout -
Bonus (thanks to Julien Deniau for the tips):
You can also
merge in the same way:
(master) $ git switch my_branch (my_branch) $ git rebase -
If you want to restore a modified file to its original state, a new command has been added since version 2.23 of git:
# Usually we use `git checkout -- <file>` (master) $ git status Changes not staged for commit: modified: README.md (master) $ git checkout -- README.md (master) $ git status nothing to commit, working tree clean # We can now use the command `git restore` (master) $ git status Changes not staged for commit: modified: README.md (master) $ git restore README.md (master) $ git status nothing to commit, working tree clean
If you liked this article, please share. I'm also writing a dev letter that you might like.