Git Basics and Configuration
Git is a distributed source control repository. For detailed knowledge, visit the official Git website.
Setting Up Git
Before using Git, you need to set up the basic configuration. This is a one-time task and ensures Git operates correctly for your environment.
Git Configuration Files
Git uses the git config
tool to get and set configuration variables. These variables can be stored in three places:
-
/etc/gitconfig
file: Applies to all users and repositories on the system. Use the --system
flag to access this file.
-
~/.gitconfig
or ~/.config/git/config
file: User-specific configurations. Use the --global
flag to access this file.
-
Repository-specific config file (
.git/config
): Specific to the repository you’re working on.
Configuring Your Identity
Command |
Description |
git config --global user.name "<UserName>" |
Sets a global username for all Git operations. |
git config --global user.email <Email_Id> |
Sets a global email for all Git commits. |
Note: Adding --global
ensures these configurations apply system-wide.
Setting Your Editor
Command |
Description |
git config --global core.editor <Text_Editor> |
Sets the default text editor for Git commit messages. |
Note: If not set, Git uses the default editor available on the system.
Checking Your Settings
Command |
Output |
Description |
git config --list |
List of configuration settings |
Displays the current Git configuration. |
Getting Help with Git
Command |
Output |
Description |
git help |
Git command usage guide |
Shows basic Git commands and usage. |
git help <command> |
Specific command usage |
Shows detailed usage of a specific command. |
Git Basic Commands
Initializing a Repository
Command |
Description |
git init |
Initializes a Git repository in an existing directory. |
git clone <url> <optional_dir_name> |
Clones an existing Git repository to a local directory. |
Managing Remote Repositories
Command |
Description |
git remote show origin |
Displays information about the remote repository. |
git remote set-url <remote> <url> |
Updates the remote repository URL. |
Recording Changes
Command |
Description |
git status |
Shows the current state of files (staged, unstaged, untracked). |
git add --all or git add <file>
|
Stages all changes or specific files for commit. |
git commit -m "<message>" |
Commits the staged changes with a message. |
Pushing and Pulling Changes
Command |
Description |
git pull |
Fetches and merges changes from the remote repository. |
git push origin <branch> |
Pushes committed changes to the specified branch on the remote repository. |
Viewing Changes
Command |
Description |
git log |
Displays detailed information about commits. |
git log --oneline |
Displays a condensed, single-line view of commits. |
Branching
Command |
Description |
git checkout -b <branch> |
Creates and switches to a new branch. |
git checkout master |
Switches back to the master branch. |
git branch -d <branch> |
Deletes the specified branch. |
Tagging
Command |
Description |
git tag |
Lists all tags in the repository. |
git tag -a <tag> -m "<message>" |
Creates an annotated tag with a message. |
Special Cases
Removing Untracked Files
Command |
Description |
git clean -f |
Removes untracked files. |
Stashing Changes
Command |
Description |
git stash |
Saves the current changes temporarily. |
git stash pop |
Reapplies the stashed changes. |
Undoing Changes
Command |
Description |
git reset HEAD <branch> |
Resets the staging area while keeping the working directory intact. |
git checkout -- <file> |
Discards changes in a file (dangerous; use cautiously). |
Top comments (0)