Skip to content

Git Commands Guide

This guide provides essential Git commands for repository management, branching, collaboration, and contributing via pull requests.


1. Create a Repository

Initialize a Local Repository
git init

This creates a new Git repository in the current directory.

Clone a Remote Repository
git clone <repository-url>

Example:

git clone https://github.com/username/repository.git

2. Branching

Create a New Branch
git branch <branch-name>

Example:

git branch new-branch
Switch to a Branch
git checkout <branch-name>

Example:

git checkout new-branch
Create and Switch to a New Branch
git checkout -b <branch-name>

Example:

git checkout -b new-branch

3. Pull Changes

Fetch and Merge Updates from the Remote Repository
git pull

This command pulls the latest changes from the default branch of the remote repository.

Pull from a Specific Branch
git pull origin <branch-name>

Example:

git pull origin main

4. Push Changes

Push a Branch to the Remote Repository
git push origin <branch-name>

Example:

git push origin new-branch
Push All Local Branches
git push --all

5. Commit Changes

Add Changes to the Staging Area
git add <filename>

Example:

git add index.html
Add All Changes
git add .
Commit Staged Changes
git commit -m "<commit-message>"

Example:

git commit -m "Update index.html structure"

6. Making a Pull Request (PR)

Steps to Create a PR:
  1. Fork the Repository (for contributing to someone else’s project):
    Go to the repository page (e.g., GitHub) and click on the Fork button.

  2. Clone Your Forked Repository:

    git clone <your-forked-repo-url>
  3. Create a New Branch:

    git checkout -b <branch-name>
  4. Make Changes and Commit:

    git add .
    git commit -m "Descriptive message about changes"
  5. Push Your Branch to Your Forked Repository:

    git push origin <branch-name>
  6. Open a Pull Request:

    • Go to the original repository page.
    • Click on Pull Requests and then New Pull Request.
    • Select your branch from your forked repository as the source and the original repository’s branch (e.g., main) as the target.
    • Add a title and description, and submit your pull request.

This guide provides a streamlined workflow for using Git effectively in collaborative and individual projects.