2.1 Git Basics: Configuration

Table of Contents

Git config

Git configuration is essential to start working with Git. The first configuration that needs to be done is setting the user name and email. This can be done using the following commands:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

The above commands set the user name and email globally. This means that these details will be used for all Git repositories that you use on your system. If you want to set this configuration locally, use the same commands without the --global option.

Configuration levels

There are three levels of Git configuration:

  • Local – Configuration for the current repository.
  • Global – Configuration for all repositories on the current user account.
  • System – Configuration for all users on the current system.

Commands for Git configuration

Here are some other useful Git configuration commands:

  • git config --list – Lists all the Git configuration settings.
  • git config --global core.editor "editor-name" – Sets the default editor for Git.
  • git config --global alias.alias-name "command" – Creates an alias for a Git command. For example, git config --global alias.co checkout sets an alias for git checkout command.

Editor settings

You can set the default editor for Git using the following command:

git config --global core.editor "editor-name"

Replace editor-name with the name of your preferred editor. For example, git config --global core.editor "nano" sets nano as the default editor for Git.

Aliases for working with Git in the console

Git command names can be lengthy, so it is useful to create aliases for commonly used commands. Here are some examples:

  • lg – Lists Git commits in a more readable format:
  • git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"
  • cm – Commits changes:
  • git config --global alias.cm "commit -m"
  • co – Checks out a branch:
  • git config --global alias.co "checkout"

View documentation from the console

You can view the Git documentation from the console using the following command:

git help

You can also view the documentation for a specific Git command using:

git help command

Replace command with the name of the command you want to view the documentation for. For example, git help commit will display the documentation for the git commit command.

Conclusion

Git configuration is an important part of working with Git. Proper configuration ensures that Git works smoothly and efficiently. By using the commands and techniques outlined in this article, you can easily configure Git for your needs.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.