Git Basics and its Commands with Examples

Github Basics

Git is a distributed version control system (DVCS) that allows developers to track changes in their codebase. It provides a way to collaborate and manage projects effectively, enabling multiple developers to work on the same project simultaneously. Git keeps a record of every change made to the code, allowing developers to easily revert to previous versions if needed. Additionally, Git facilitates branching, merging, and conflict resolution, making it a powerful tool for software development. It is widely used in the industry, especially in open-source projects, thanks to its versatility and efficiency.


To create a local copy of a remote repository, you can use the git clone command. This command allows you to download the entire repository from a remote location and create a copy on your local machine.

Here’s how to do it:

  1. Open your terminal or command prompt.
  2. Navigate to the directory where you want to store the local copy of the repository.
  3. Use the git clone command followed by the URL of the remote repository.
  4. Press Enter to execute the command.

Git will then download all the files and history from the remote repository and create a new folder with the repository name in your current directory. You can then navigate into the repository folder and start working with the code or files locally.

Remember to ensure that you have Git installed on your machine before using the git clone command.

git clone <repository_url>

Replace username/repository with the actual username and repository name.


Once inside the repository directory, run the following command to create a new branch:

git branch <branch name>

To switch to a newly created branch in Git, you can use the following command. Replace <branch-name> with the actual name of the branch you want to switch to.

git checkout <branch name>


To create a commit with changes and a commit message, Use the command git add to add the changes you want to include in the commit.

git commit -m "Commit message"

After executing the commit command, Git will create a new commit with the changes you added and the specified commit message. This commit will be part of your project’s version history.

Remember that committing your changes regularly is a good practice to keep track of your work and make it easier to collaborate with others.


To update your local repository with changes from a remote repository, you can use the git pull command. This command fetches the latest changes from the remote repository and merges them into your local branch.

git pull


To upload your local changes to the remote repository, you can use the git push command. This command allows you to push your local commits to the remote repository, syncing your changes with the remote branch.

Here’s how to do it:

git push <remote-name> <branch-name>

Force Push:

Git force push is a command used to update a remote repository by forcefully overwriting the remote branch with your local branch. It is a more aggressive and potentially risky option compared to a regular push.

When you perform a force push, Git discards all changes in the remote branch and replaces them with the contents of your local branch, regardless of whether they conflict or not. This can be useful in certain scenarios, such as when you want to undo or reset previous commits, or when you need to synchronize your local branch with the latest changes from other branches.

To use force push, you need to follow these steps:

  1. First, ensure that you have committed all your changes locally using the git commit command.
  2. Then, execute the force push command using the git push command with the --force or -f flag, followed by the remote name and the branch name. For example:

    git push --force origin main

    This command forcefully overwrites the remote branch named “main”.

It’s important to note that force pushing can overwrite commits, and if other team members are also working on the same remote branch, it can lead to conflicts and loss of their work. Therefore, it is generally recommended to use force push with caution, especially when collaborating with others. Communication and coordination with your team are essential to avoid unintentional conflicts or data loss.

If you are uncertain about using force push, it is recommended to consult with your team or refer to Git’s documentation for more information on when and how to use this command appropriately.


To merge changes from one branch into another in Git, you can follow these steps:

  1. Checkout the branch that you want to merge changes into. For example, if you want to merge changes into the main branch, you can use the following command:

    git checkout main
  2. Run the merge command, specifying the branch you want to merge from. For example, if you want to merge changes from a branch called feature-branch, you can use the following command:
    git merge feature-branch 

    Git will attempt to merge the changes from the specified branch into the current branch. If there are any conflicts between the branches, you will need to resolve them manually.

  3. Resolve any conflicts that may arise during the merge process. Git will automatically mark the conflicted files and insert conflict markers (<<<<<<<, =======, and >>>>>>>) in the affected files. Open the conflicted files, locate the conflicting sections, and modify them to resolve the conflicts. Once you have resolved all conflicts, save the files.
  4. After resolving the conflicts, add the modified files to the staging area using the
    git add command: git add <file1> <file2> ... 

    Replace <file1>, <file2>, etc. with the names of the conflicted files.

  5. Finally, create a new commit to finalize the merge:
    git commit -m "Merge changes from feature-branch" 

    Git will create a new commit that combines the changes from the merged branch into the current branch.

Pull Request:

Creating a pull request is typically done through a platform like GitHub or GitLab. Below is the overview of the process involved in creating a pull request using such platforms:

  1. Fork the Repository: If you don’t have write access to the original repository, you’ll need to fork it. This will create a copy of the repository under your GitHub or GitLab account.
  2. Clone the Forked Repository: Once you have forked the repository, you’ll need to clone it to your local machine using the git clone command. This will create a local copy of the forked repository on your machine.
  3. Create a New Branch: Before making any changes, create a new branch for your changes. This helps keep your changes isolated and makes it easier to manage them. Use the git branch command to create a new branch, and then use git checkout to switch to that branch.
  4. Make and Commit Changes: Make the necessary changes to the codebase in your local repository. Once you’re satisfied with the changes, use the git add command to stage the changes, and then use git commit to create a new commit with a descriptive commit message.
  5. Push the Branch: After committing your changes locally, push the branch to your forked repository using the git push command. This will make the branch and its commits available on the remote repository.
  6. Create the Pull Request: Now, navigate to the original repository on GitHub or GitLab, and you should see an option to create a pull request. Click on it, and you’ll be prompted to compare and select the branch you just pushed from your forked repository.
  7. Review and Submit the Pull Request: In the pull request interface, you can review the changes made in the branch, leave comments, and discuss with other collaborators. If everything looks good, click on the submit button to create the pull request.

From this point forward, the project maintainers will review your changes, provide feedback, and eventually merge your changes into the original repository if they find them suitable.

Please note that the exact process may vary depending on the platform and repository you are using.


HEAD in Git is a pointer to the currently checked-out branch or commit. It is a symbolic reference to the latest commit in the current branch. When you make a new commit, HEAD is automatically updated to point to the newly created commit.

HEAD is a key concept in Git because it allows you to track your progress and make changes to your codebase in a controlled manner. For example, you can use HEAD to:

  • See the current state of your codebase
  • Revert to a previous state of your codebase
  • Merge changes from other branches
  • Publish your changes to a remote repository

HEAD is typically located in the .git/HEAD file in your Git repository. However, you should not manually edit this file, as it can corrupt your repository.

In addition to the main HEAD pointer, Git also supports detached HEAD. A detached HEAD is a state where HEAD is not pointing to any branch, but instead points to a specific commit. This can be useful for tasks such as comparing different versions of your codebase or cherry-picking commits from one branch to another.

Here are some examples of how to use HEAD in Git:

# View the current state of your codebase
git status

# Revert to a previous state of your codebase
git reset HEAD~

# Merge changes from another branch
git merge another-branch

# Publish your changes to a remote repository
git push origin master

HEAD is an essential concept in Git, and understanding how it works is key to using Git effectively.

Git rm:

The git rm command is used to remove files or directories from a Git repository. It is used to stage the removal of files so that they are no longer tracked by Git.

Here’s how to use the git rm command:

  1. Open your terminal or command prompt.
  2. Navigate to the root directory of your Git repository.
  3. Run the following command to remove a file:

    git rm <file>

    Replace <file> with the name of the file you want to remove.

  4. If you want to remove a directory and its contents, use the following command:

    git rm -r <directory>

    Replace <directory> with the name of the directory you want to remove.

  5. Once you have run the git rm command, Git will stage the removal of the specified file or directory. To permanently remove the file or directory from your repository, you need to commit the changes using the git commit command:

    git commit -m "Remove <file/directory>"

    Replace <file/directory> with the name of the file or directory you removed.

    Note: If you only want to stop tracking a file or directory but keep it in your local filesystem, you can use the --cached option with the git rm command:

    git rm --cached <file/directory>

    This will remove the file or directory from Git’s tracking but leave it in your local filesystem.

Remember to be cautious when using the git rm command, as it permanently removes files or directories from your repository’s history. Make sure to double-check your changes and have proper backups if needed.

Thank you for reading. Will share some another advanced Git features in upcoming post.