Git Commands: A Beginner's Guide - I

Git is a version control system that allows developers to track changes in their codebase over time. It is a powerful tool that can help you collaborate with others, keep track of your work, and roll back changes when necessary. In this article, we will cover some of the most common Git commands that you will need to know as a beginner.

Setting Up Git


Before you can start using Git, you will need to install it on your computer. You can download Git from the official website and follow the installation instructions for your operating system.

Once you have Git installed, you will need to configure your name and email address so that Git can identify you as the author of your commits. You can do this by running the following commands:

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

Creating a Repository


A repository is a container for your project files and the history of changes made to them. To create a new repository, navigate to the directory where you want to store your project and run the following command:

git init

This will create a new repository in the current directory. You can now start adding files to your repository and tracking changes.

Adding Files


To add files to your repository, use the git add command followed by the name of the file or directory you want to add. For example, to add a file called main.c, run the following command:

git add main.c

You can also add all files in a directory by running git add followed by the directory name:

git add mydirectory/

Committing Changes


Once you have added files to your repository, you need to commit them to save the changes. To commit changes, use the git commit command followed by a commit message that describes the changes you made. For example:

git commit -m "Added main.c"

This will create a new commit with the message "Added main.c". You should always include a descriptive commit message so that others can understand what changes were made.

Viewing History


You can view the history of changes in your repository using the git log command. This will display a list of all commits in reverse chronological order, with the most recent commit at the top:

git log

You can also view the changes made in a specific commit using the git show command followed by the commit hash:

git show abc123

Branching and Merging


Branching allows you to create a separate copy of your codebase to work on without affecting the main branch. This is useful for experimenting with new features or making changes that you are not sure about.

To create a new branch, use the git branch command followed by the name of the new branch:

git branch mynewbranch

You can then switch to the new branch using the git checkout command:

git checkout mynewbranch

Once you have made changes on your new branch, you can merge them back into the main branch using the git merge command:

git checkout main
git merge mynewbranch

This will merge the changes from mynewbranch into main.

Conclusion


Git is a powerful tool that can help you keep track of changes in your codebase and collaborate with others. By learning these basic Git commands, you will be able to get started with version control and begin using Git in your own projects.

Did you find this article valuable?

Support Mithilesh Gaikwad by becoming a sponsor. Any amount is appreciated!