A Beginner’s Guide to Git and GitHub: Key Commands and Their Uses

Version control is an essential part of every developer’s workflow. Recently, I learned how to use Git and GitHub, and I wanted to share the basic commands I came across and their uses in simple terms.

Let’s dive in!


📋 Getting Started

  1. git init
    Initializes a new Git repository in your project directory.

  2. git status
    Displays the status of your working directory and staging area, showing changes tracked and untracked by Git.

  3. git add <file>
    Stages changes for the next commit. Use git add . to stage all changes.

  4. git commit -m "message"
    Records changes to the repository with a descriptive message.


🌐 Working with Remote Repositories

  1. git remote add origin <url>
    Links your local repository to a remote repository hosted online.

  2. git remote -v
    Displays the URLs of the remote repositories you’ve added.

  3. git push
    Uploads your local commits to a remote repository (e.g., GitHub).

  4. git pull
    Fetches and merges changes from a remote repository into your local branch.

  5. git fetch
    Fetches changes from a remote repository without merging them.

  6. git clone <url>
    Creates a local copy of a remote repository.


🔄 Branch Management

  1. git branch
    Lists all branches in your repository.

  2. git branch -m <new-branch-name>
    Renames the current branch.

  3. git branch -d <branch-name>
    Deletes the specified branch.

  4. git switch <branch-name>
    Switches to the specified branch.

  5. git switch -c <new-branch-name>
    Creates and switches to a new branch.

  6. git merge <branch-name>
    Combines changes from the specified branch into the current branch.


🧰 Other Useful Commands

  1. git stash
    Temporarily stores uncommitted changes so you can work on something else.

  2. git stash pop
    Reapplies the most recent stash and removes it from the stash list.

  3. git diff
    Shows differences between your working directory and the index or between commits.

  4. git rebase
    Reapplies commits on top of another branch, helping to maintain a clean commit history.

  5. git reflog
    Displays a log of all reference updates, like branch checkouts and commits.


✏️ Managing Files

  1. .gitignore
    Specifies files and directories for Git to ignore.

  2. gitkeep
    Used to track empty directories in a Git repository, as Git doesn’t track empty folders by default.


Wrapping Up

These commands are the backbone of version control with Git and GitHub. As you practice, you’ll discover how they simplify collaboration and improve code management.

What’s your favorite Git command? Share it below!