Table of content

Setting Up

Git Configuration

Basic Commands

Checking Repository Status

Staging Changes

Committing Changes

Viewing Logs

Branching

Cloning a Repository

Pushing Changes

Pulling Changes

Conclusion

Basic Git Commands for Beginners

November 02, 2024

Git is an essential tool for version control, allowing developers to manage and track code changes efficiently. Whether you're working solo or as part of a team, mastering basic Git commands will make your life easier. Here’s a quick guide to get you started with the most commonly used Git commands.


Setting Up

Before you start, make sure Git is installed on your computer. You can download it from git-scm.com.

Once installed, open your terminal or command prompt to start using Git commands.

Git Configuration

Begin by configuring your Git environment with your name and email:

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

These commands set your global username and email address, which will appear in your commits.

Basic Commands

Initializing a Repository To create a new Git repository in your current directory, use:

git init

This command initializes a .git directory, where Git stores all repository-related information.

Checking Repository Status

The git status command displays the state of the working directory and staging area. It tells you which files have been modified, staged, or remain untracked.

git status

Staging Changes

To stage a file for commit, use:

git add <filename>

Or to stage all modified files:

git add .

Staging allows you to specify which changes to commit.

Committing Changes

Once changes are staged, you can commit them to your repository. Include a descriptive message with each commit:

git commit -m "Your commit message"

Viewing Logs

To view the commit history, use:

git log

This command shows a list of past commits, including details such as commit ID, author, date, and commit message.

Branching

Branches allow you to work on different features or fixes independently. To create a new branch:

git branch <branch-name>

Switch to a branch using:

git checkout <branch-name>

Or create and switch to a branch in one step:

git checkout -b <branch-name>

Working with Remote Repositories

Cloning a Repository

To clone a repository from a remote source like GitHub:

git clone <repository-url>

This command creates a local copy of the remote repository on your computer.

Pushing Changes

To push changes from your local repository to a remote repository:

git push origin <branch-name>

Replace with the branch you want to push. origin is the default name for the remote repository.

Pulling Changes

To fetch and integrate changes from a remote repository:

git pull

This command combines fetch and merge, pulling in new changes and merging them into your local branch.

Conclusion

These are the basic Git commands you need to start managing code versions efficiently. As you become more comfortable with Git, you'll find additional commands and options that make collaborating on projects even easier.

Happy coding!