10+ Powerful Git Commands to Master Version Control for Beginners

Git
Git

Git and GitHub are essential tools for modern software development, enabling version control and collaborative work on code. This guide will walk you through the steps to get started with Git Commands But if you don’t know what is Git and GitHub then you should read this article 1st click here to read

Step 1: Install Git & Create an Account on GitHub

  1. Before you can start using Git, you need to install it on your computer. You can download Git from the official website.
  2. Go to GitHub Signup.

Step 3: Setting Up Your Project

After installing Git, you should use Git Bash to navigate and manage your project. Open Git Bash in your project folder and follow these steps to create and initialize a new project.

Quick Guide: Navigating Your Project Folder in Git Bash

  • See the current location:
  $ pwd

The pwd (print working directory) command displays the current directory path.

  • List the files and folders in the current directory:
  $ ls

The ls command lists the files and folders in the current directory.

  • List all files, including hidden ones:
  $ ls -a

The ls -a command lists all files, including hidden files (those starting with a dot).

  • Change directory:
  $ cd <directory_name>

The cd (change directory) command moves you into the specified directory.

  • Go back to the previous directory:
  $ cd ..

The cd .. command moves you up one directory level.

Setting Up Git Configuration

  1. Verify Git installation:
   $ git --version

This command checks if Git is installed and displays the installed version.

  1. Set your user name and email:
   $ git config --global user.name "sagar"
   $ git config --global user.email "sagar@gmail.com"

These commands set your user name and email, which will be associated with your Git commits.

  1. Edit global configuration directly:
   $ git config --global --edit

This command opens the global Git configuration file in the default text editor. To exit the editor, press ESC, then type :wq and press Enter.

Initialize a Git Repository

  1. Navigate to your project folder:
   $ cd gitlearneasy

Use cd to change the directory to your project folder.

  1. Initialize an empty Git repository:
   $ git init

This command creates a new Git repository in your project folder.

  1. List files in the directory to verify repository creation:
   $ ls
   $ ls -a

ls lists the files in the directory, and ls -a includes hidden files, such as the .git directory created by git init.

Step 4: Create and Commit Files

  1. Create a file named Test.java and add some code to it.
  2. Check the status of your files:
   $ git status

This command shows the status of the working directory and staging area. It displays which changes have been staged, which haven’t, and which files aren’t being tracked by Git.

  1. Add Test.java to the staging area:
   $ git add Test.java

This command stages changes to be committed. Staging is like preparing a package to send.

  1. Verify the status again:
   $ git status

This will show that Test.java is now staged.

  1. Commit the file to the local repository with a message:
   $ git commit -m "first comment"

The commit command records the changes to the repository. The -m option lets you add a commit message inline.

  1. View your commit history:
   $ git log

This command shows the commit history. Each commit will have a unique hash code.

Adding More Code

  1. Modify Test.java and stage the changes:
   $ git add Test.java
   $ git commit -m "second comment"

These commands stage and commit the changes made to Test.java.

  1. To add all changes in the project at once:
   $ git add .
   $ git commit -m "third comment"

git add . stages all changes in the working directory, and git commit -m "message" commits them with a message.

Step 5: Reverting Changes

Sometimes, you may need to revert your project to a previous state. This can be done using the commit hash code. Here’s how you can navigate through your commit history and revert to a specific point.

Checkout a Previous Commit

  1. View your commit history:
   $ git log

This command displays the commit history, showing commit hashes, authors, dates, and commit messages.

  1. Switch to a previous commit:
   $ git checkout <commit HASH code>

Replace <commit HASH code> with the specific hash of the commit you want to revert to. This command changes the working directory to match the state of the specified commit. Note that this puts your repository into a “detached HEAD” state, meaning you are not on any branch.

Example:

   $ git checkout bad9c532fa5188e13a539901a0222f9df06ad650

In this example, the working directory is set to the state of the commit with the hash bad9c532fa5188e13a539901a0222f9df06ad650.

Working in Detached HEAD State

While in this state, you can explore the project as it was at the specific commit. However, any changes you make won’t be reflected in any branch until you create a new branch or switch back to an existing branch and merge the changes.

  1. Create a new branch from this state (optional):
   $ git checkout -b new-branch-name

This command creates a new branch and switches to it, allowing you to preserve the state from the specific commit and continue working from there.

Return to the Latest Commit on the Master Branch

  1. Switch back to the master branch:
   $ git checkout master

This command returns you to the latest commit on the master branch, leaving the detached HEAD state.

  1. Update your master branch if needed:
   $ git pull origin master

This command updates your local master branch with any new changes from the remote repository, ensuring you have the latest version of the project.

By understanding how to revert to previous commits and work with the commit history, you can effectively manage and recover your project’s state at any point in time.

Step 6: Working with Branches

Creating and Switching Branches

  1. List all local branches:
   $ git branch

This command lists all the local branches in the repository.

  1. Create a new branch:
   $ git branch dev

This command creates a new branch named dev.

  1. Switch to the new branch:
   $ git checkout dev

This command switches to the dev branch.

  1. Create and switch to a new branch in one step:
   $ git checkout -b ram/teacher

The -b option creates a new branch and switches to it.

Making Changes in a New Branch

  1. Create a new file Teacher.java and add some code.
  2. Stage and commit the new file:
   $ git add Teacher.java
   $ git commit -m "add teacher class"

Merging Branches

  1. Switch back to the dev branch:
   $ git checkout dev
  1. Merge changes from ram/teacher into dev:
   $ git merge ram/teacher
  1. To merge dev into master:
   $ git checkout master
   $ git merge dev

Step 7: Pushing to GitHub

  1. Log in to your GitHub account and create a new repository named gitlearneasy.
  2. Add a description if desired and create the repository.

Connecting Local Repository to GitHub

  1. Back in your project directory, check for connected remote repositories:
   $ git remote -v

This command shows the URLs of remote repositories.

  1. Add the GitHub repository as a remote:
   $ git remote add origin https://github.com/sagar/Student_Management_cpp.git

This command adds a remote repository named origin.

  1. Rename the default branch to main if necessary:
   $ git branch -M main

This command renames the current branch to main.

  1. Push your local repository to GitHub:
   $ git push -u origin main

This command pushes the local changes to the main branch of the remote repository.

Resolving Push Rejections

If you encounter a rejection error, integrate remote changes before pushing:

  1. Pull and rebase the remote changes:
   $ git pull --rebase origin main

This command fetches and integrates changes from the remote repository.

  1. Push the local repository to GitHub:
   $ git push origin main

By following these steps, you’ll have a solid foundation in using Git and GitHub for version control and collaboration. As you become more comfortable, you can explore additional features and workflows to enhance your development process. Happy coding!