Top 10 GitHub Commands Every Developer Should Know

08 Jan 2025 03:35 PM

Top 10 GitHub Commands Every Developer Should Know

GitHub is a powerful tool for developers to collaborate, track changes, and maintain codebases. To leverage its full potential, understanding the underlying Git commands is essential. Below is a detailed guide to the top 10 Git commands, including their purpose, syntax, and step-by-step explanations.


1. git init

Purpose:

Initializes a new Git repository in your project directory, enabling Git to start tracking changes.

Step-by-Step:

  1. Open a terminal and navigate to your project folder.
     
    cd /path/to/your/project
     
  2. Run the following command to initialize a Git repository:
    git init
    
    
  3. Git creates a hidden .git directory where it stores all metadata and versioning information.

Tip: Use git init only once per project. For existing repositories, skip this step and use git clone instead.


2. git clone

Purpose:

Downloads an existing repository from GitHub (or any remote Git server) to your local machine.

Syntax:

git clone <repository-URL>

Step-by-Step:

  1. Find the repository URL on GitHub. Example URL:
    https://github.com/user/repo.git
    
  2. In your terminal, run:
    git clone https://github.com/user/repo.git
    
    
  3. Git creates a local copy of the repository in a new folder named after the repository.

Tip: Use git clone <URL> <custom-folder-name> to specify a different folder name.


3. git add

Purpose:

Stages changes (new, modified, or deleted files) to prepare them for a commit.

Syntax:

  • Add a specific file:
    git add <file-name>
    
    
  • Add all files in the current directory:
    git add .
    
    

Step-by-Step:

  1. After making changes to files, check which files have been modified:
    git status
    
    
  2. Stage the changes:
    git add .
    
    
  3. Run git status again to confirm the files are staged.

Tip: Staging files lets you commit them selectively.


4. git commit

Purpose:

Captures a snapshot of the staged changes, marking a version in your project’s history.

Syntax:

git commit -m "Your commit message"

Step-by-Step:

  1. Ensure changes are staged using git add.
  2. Write a meaningful commit message describing your changes:
    git commit -m "Fix login bug and improve error messages"
    
    

Tip: Use git commit -am "message" to stage and commit changes in a single step (only for tracked files).


5. git status

Purpose:

Shows the current state of your working directory and staging area.

Syntax:

git status

Output Information:

  • Untracked files: Files that Git doesn’t yet manage.
  • Changes not staged for commit: Modified files that aren’t staged.
  • Changes to be committed: Files staged and ready for commit.

Tip: Use this command frequently to stay informed about the status of your repository.


6. git pull

Purpose:

Fetches the latest changes from the remote repository and merges them into your local branch.

Syntax:

git pull origin <branch-name>

Step-by-Step:

  1. Identify your current branch using:
    git branch
    
    
  2. Pull updates from the remote repository:
    git pull origin main
    
    

Tip: Resolve any merge conflicts that may arise using a merge tool or manually.


7. git push

Purpose:

Uploads local changes to a remote repository, making them accessible to collaborators.

Syntax:

git push origin <branch-name>

Step-by-Step:

  1. Commit your changes using git commit.
  2. Push changes to the remote branch:
    git push origin main
    

Tip: Use git push -u origin <branch-name> when pushing a new branch for the first time.


8. git branch

Purpose:

Manages branches, allowing you to work on different features or fixes simultaneously.

Syntax:

  • List all branches:
    git branch
    
    
  • Create a new branch:
    git branch <branch-name>
    
    
  • Delete a branch:
    git branch -d <branch-name>
    
    

Step-by-Step:

  1. Create a branch for a new feature:
    git branch feature-login
    
    
  2. Switch to the new branch:
    git checkout feature-login
    
    

Tip: Keep your main branch clean by merging completed features into it.


9. git checkout

Purpose:

Switches between branches or restores a specific file to its last committed state.

Syntax:

  • Switch branches:
    git checkout <branch-name>
    
    
  • Restore a file:
    git checkout -- <file-name>
    
    

Step-by-Step:

  1. Switch to an existing branch:
    git checkout main
    
    
  2. Restore a modified file to its previous version:
    git checkout -- index.html
    
    

Tip: Use git switch (a newer command) instead of git checkout to switch branches.


10. git merge

Purpose:

Combines changes from one branch into another.

Syntax:

git merge <branch-name>

Step-by-Step:

  1. Switch to the branch you want to merge changes into:
    git checkout main
    
    
  2. Merge the changes from another branch:
    git merge feature-login
    
    

Tip: Resolve conflicts during a merge by manually editing conflicting files.


Bonus Commands

  1. git log: View commit history.

    git log
    
    

    Use git log --oneline for a concise summary.

  2. git diff: Compare changes between commits or branches.

    git diff
    
    
  3. git stash: Temporarily save changes without committing.

    git stash
    
    

    Restore changes later using:

    git stash apply
    
    

Mastering these commands will not only help you navigate GitHub more effectively but also make you a more efficient collaborator in any project. Practice using them in real-world scenarios to solidify your understanding!

0
14