Git Commands

Vlad O.

Updated:

Git and its commands is essential for efficient version control and collaboration. Git provides a plethora of commands to manage your codebase effectively. 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:

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:

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:

git init

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

Example:

git init

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:

git clone

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

Example:

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:

git status

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

Example:

git status

Staging Changes

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

git add

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

Example:

git add index.html

Committing Changes

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

git commit -m "Your commit message here"

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

Example:

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

Viewing Commit History

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

git log

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

Example:

git log

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:

git branch

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

Example:

git branch feature-login

Switching Branches

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

git checkout

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

Example:

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:

git merge

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

Example:

git merge feature-login

Pushing Changes

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

git push

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

Example:

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:

git pull

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

Example:

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:

git add

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

Example:

git add index.html

Remove branches

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

git config --get remote.origin.url

git remote show [remote-name] command

git remote show origin

git remote -v

Posted in Git tagged as github terminal