GitHub Commands with Examples

Estimated read time 4 min read

Summary:

GitHub commands are essential for managing version control in software development. This guide provides a list of commonly used Git commands, explaining when and how to use them with practical examples. These commands help in repository management, branching, collaboration, and maintaining code versions effectively.

Basic Commands:

  1. Clone a Repository:
  • Command: git clone <repository_url>
  • Example:
    sh git clone https://github.com/username/repository.git
  • Usage: Use this command to create a local copy of a remote repository.
  1. Check Repository Status:
  • Command: git status
  • Example:
    sh git status
  • Usage: Use this command to see the status of changes in your working directory.
  1. Add Changes to Staging Area:
  • Command: git add <file_name>
  • Example:
    sh git add index.html
  • Usage: Use this command to stage changes for the next commit. Use git add . to add all changes.
  1. Commit Changes:
  • Command: git commit -m "commit message"
  • Example:
    sh git commit -m "Added new feature to homepage"
  • Usage: Use this command to commit staged changes with a message describing the changes.
  1. Push Changes to Remote Repository:
  • Command: git push
  • Example:
    sh git push origin main
  • Usage: Use this command to push committed changes to the remote repository.

Branching:

  1. Create a New Branch:
  • Command: git branch <branch_name>
  • Example:
    sh git branch feature-new-homepage
  • Usage: Use this command to create a new branch.
  1. Switch to a Branch:
  • Command: git checkout <branch_name>
  • Example:
    sh git checkout feature-new-homepage
  • Usage: Use this command to switch to the specified branch.
  1. Merge a Branch:
  • Command: git merge <branch_name>
  • Example:
    sh git merge feature-new-homepage
  • Usage: Use this command to merge changes from the specified branch into the current branch.

Collaboration:

  1. Pull Changes from Remote Repository:
  • Command: git pull
  • Example:
    sh git pull origin main
  • Usage: Use this command to fetch and merge changes from the remote repository to your local repository.
  1. Fork a Repository:
  • Action: Click “Fork” button on GitHub.
  • Example: Fork a repository to create your copy in your GitHub account.
  • Usage: Use this feature to contribute to a project by making changes in your forked repository and then creating a pull request.
  1. Create a Pull Request:
  • Action: Go to your forked repository on GitHub, click “New pull request”.
  • Example: Create a pull request to propose your changes to the original repository.
  • Usage: Use this to request merging your changes from your forked repository to the original repository.

Repository Management:

  1. Initialize a New Git Repository:
  • Command: git init
  • Example:
    sh git init
  • Usage: Use this command to initialize a new Git repository in your current directory.
  1. Add a Remote Repository:
  • Command: git remote add origin <repository_url>
  • Example:
    sh git remote add origin https://github.com/username/repository.git
  • Usage: Use this command to add a remote repository to your local repository.
  1. View Commit History:
  • Command: git log
  • Example:
    sh git log
  • Usage: Use this command to view the commit history of the repository.

Examples in Use:

  1. Cloning and Initial Setup:
  • Clone a repository:
    sh git clone https://github.com/username/repository.git
  • Navigate into the repository and check status:
    sh cd repository git status
  1. Creating and Working on a New Branch:
  • Create a new branch and switch to it:
    sh git branch feature-new-homepage git checkout feature-new-homepage
  1. Committing and Pushing Changes:
  • Make changes, add them to staging, commit, and push:
    sh git add . git commit -m "Added new feature to homepage" git push origin feature-new-homepage
  1. Collaborating with Others:
  • Pull latest changes from the main branch:
    sh git pull origin main
  1. Merging Changes:
  • Merge feature branch into main:
    sh git checkout main git merge feature-new-homepage

These commands cover essential Git operations for managing repositories, branching, and collaboration on GitHub, enhancing productivity and workflow efficiency in version control.

+ There are no comments

Add yours