Skip to main content

Git Commands

Git and its commands is essential for efficient version control and collaboration. Git provides a plethora of commands to manage your codebase effectively. For guidance on organizing your work across teams and releases, see our guide on branching strategies. In this comprehensive guide, we'll explore various Git commands, what they are used for, and provide engaging examples of how to use them.

What is Git?

Before diving into Git commands, let's briefly understand what Git is. Git is a distributed version control system designed to track changes in your codebase and facilitate collaboration among developers.

Getting Started with Git

If you haven't installed Git yet, you can download and install it from the official Git website. Once installed, you can start using Git via the command line or by utilizing GUI-based applications like GitHub Desktop or GitKraken.

Configuring Git

Before using Git for the first time, it's crucial to configure your identity. Use the following commands to set your username and email:

Setting Username and Email

bash
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Explanation: These commands set your global username and email, which will be used for all your Git commits. It helps in identifying the author of each commit accurately.

Example:

bash
git config --global user.name "John Doe"
git config --global user.email "john.doe@example.com"

Creating a New Repository

Git allows you to create a new repository for your project. Use the following command to initialize a new Git repository:

bash
git init

Explanation: This command initializes an empty Git repository in the current directory, enabling version control for your project.

Cloning a Repository

If you want to work on an existing project, you can clone it from a remote repository. Use the following command to clone a repository:

bash
git clone <repository-url>

Explanation: This command clones the repository located at the specified remote URL to your local machine.

Example:

bash
git clone https://github.com/exampleuser/example-repo.git

Checking the Status

Always know the current status of your repository. Use the following command to check the status of your working directory:

bash
git status

Explanation: This command displays the current state of your working directory, showing which files are modified, added, or deleted.

Staging Changes

Before committing changes, you need to stage them. Use the following command to stage specific files:

bash
git add <file>

Explanation: This command stages the specified file, preparing it for the commit.

Example:

bash
git add index.html

Committing Changes

After staging changes, it's time to commit them. Use the following command to create a commit:

bash
git commit -m "Your commit message here"

Explanation: This command creates a commit with the staged changes and adds a descriptive commit message.

Example:

bash
git commit -m "Added new feature: User authentication"

Viewing Commit History

To view the commit history of your repository, use the following command:

bash
git log

Explanation: This command displays a chronological list of commits, showing commit messages, authors, timestamps, and unique commit IDs.

Creating Branches

Git allows you to work on different branches to develop features or fix bugs independently. Use the following command to create a new branch:

bash
git branch <branch-name>

Explanation: This command creates a new branch with the specified name.

Example:

bash
git branch feature-login

Switching Branches

To switch to a different branch, use the following command:

bash
git checkout <branch-name>

Explanation: This command switches your working directory to the specified branch.

Example:

bash
git checkout feature-login

Merging Branches

Once you've completed work on a feature branch, you can merge it into the main branch. Use the following command to merge branches:

bash
git merge <branch-name>

Explanation: This command merges the specified branch into the current branch.

Example:

bash
git merge feature-login

Pushing Changes

To share your local commits with the remote repository, use the following command:

bash
git push <remote> <branch>

Explanation: This command pushes your local commits to the specified remote repository and branch.

Example:

bash
git push origin main

Pulling Changes

If other developers have made changes to the remote repository, you can pull those changes to your local repository. Use the following command:

bash
git pull <remote> <branch>

Explanation: This command fetches and merges changes from the specified remote repository and branch into your current branch.

Example:

bash
git pull origin main

Handling Conflicts

Conflicts can occur when merging or pulling changes. Git will prompt you to resolve conflicts manually. After resolving, use the following command to mark the conflicts as resolved:

bash
git add <resolved-file>

Explanation: This command stages the resolved file, indicating that conflicts have been resolved.

Example:

bash
git add index.html

Remove branches

bash
git push origin --delete <branchName>   # delete remote branch
git branch -D <branchName>              # delete local branch
git pull --rebase
git reset --hard HEAD

Check remote repository

bash
git config --get remote.origin.url
git remote show <remote-name>
git remote show origin
git remote -v

Related Articles