CodingBowl

Mastering Git: Your Essential Guide to Pull, Commit, Merge, Push, Branch, and Switch

Published on 25 Jul 2025DevOps
image
Photo by Steve Johnson on Unsplash

Git is an indispensable version control system for developers, enabling seamless collaboration and robust tracking of project changes. This guide provides a concise yet comprehensive overview of the fundamental Git commands you'll use daily: pull, commit, merge, push, branch, and switch.


1. git pull: Staying Up-to-Date

git pull is your command to synchronize your local repository with changes from a remote repository. It's a two-step operation:

  • Fetches (downloads) new commits, branches, and tags from the remote.
  • Merges those fetched changes into your current local branch.

When to use it: Always run git pull before starting new work or pushing your own changes to ensure you're working with the latest version of the codebase.

How to use it:

git pull

Pro-Tip: To maintain a clean, linear commit history, consider using git pull --rebase. This re-applies your local commits on top of the fetched changes, avoiding extra merge commits.

git pull --rebase

2. git commit: Saving Your Changes

git commit records your changes to the local repository. Each commit represents a snapshot of your project at a specific point in time, along with a descriptive message explaining what was changed.

When to use it: After making logical changes to your code, stage them, and then commit them. Aim for small, focused commits that address a single concern.

How to use it:

  1. Stage your changes:
    git add . # Stages all changes in the current directory
    # or
    git add <file-name> # Stages a specific file
  2. Commit your staged changes:
    git commit -m "Your descriptive commit message here"
    • The -m flag allows you to provide a commit message directly in the command. For longer messages, omit -m, and Git will open your default text editor.

3. git merge: Integrating Branches

git merge is used to combine the history of one branch into another. This is crucial for integrating features developed on separate branches back into the main codebase.

When to use it: When a feature branch is complete and ready to be integrated into a stable branch (e.g., main or develop).

How to use it:

  1. Switch to the branch you want to merge into:
    git switch main # Assuming you want to merge into 'main'
  2. Merge the other branch into the current branch:
    git merge <branch-to-merge-from>

    For example, to merge a feature/new-feature branch into main:

    git switch main
    git merge feature/new-feature

Handling Conflicts: If Git encounters conflicting changes during a merge, it will pause and notify you. You'll need to manually resolve these conflicts in the affected files, then git add the resolved files, and finally git commit to complete the merge.


4. git push: Sharing Your Work

git push uploads your local commits to a remote repository, making your changes available to others.

When to use it: After committing your changes locally, push them to the remote to share your work with the team or to back up your progress.

How to use it:

git push

First-time push for a new branch: If you're pushing a newly created local branch for the first time, you might need to set its upstream (tracking) branch:

git push -u origin <your-branch-name>
  • The -u (or --set-upstream) flag tells Git to remember the relationship between your local branch and the remote branch, so future git push and git pull commands are simpler.

5. git branch: Managing Development Lines

git branch allows you to create, list, and delete branches. Branches are essential for parallel development, enabling multiple developers to work on different features or fixes simultaneously without interfering with each other's work.

When to use it: Create a new branch for every new feature, bug fix, or experimental change. This isolates your work and keeps the main branch stable.

How to use it:

  • Create a new branch:
    git branch <new-branch-name>
  • List all branches (local and remote):
    git branch -a
  • Delete a branch (after it's merged and no longer needed):
    git branch -d <branch-to-delete>
    • Use -D (uppercase) to force delete an unmerged branch (use with caution!).

6. git switch: Navigating Branches

git switch (introduced as a more intuitive alternative to git checkout for branch switching) allows you to navigate between different branches in your repository.

When to use it: To move between your feature branches, the main development branch, or any other branch in your project.

How to use it:

  • Switch to an existing branch:
    git switch <branch-name>
  • Create and switch to a new branch in one command:
    git switch -c <new-branch-name>
    • This is a convenient shortcut for git branch <new-branch-name> followed by git switch <new-branch-name>.

7. git remote -v: Checking Your Remote Connections

It's often useful to know which remote repositories your local project is connected to. The git remote -v command shows you the shortnames of your remote repositories along with their URLs for both fetching (downloading) and pushing (uploading) changes.

When to use it: To verify the origin of your repository, check for multiple remotes (e.g., origin and upstream), or confirm the URLs you're interacting with.

How to use it:

git remote -v

Example Output:

origin  https://github.com/your-username/your-repo.git (fetch)
origin  https://github.com/your-username/your-repo.git (push)
upstream https://github.com/original-author/original-repo.git (fetch)
upstream https://github.com/original-author/original-repo.git (push)

In this example, origin is the primary remote, and upstream might refer to an original repository if you forked the project.


8. git status: Understanding Your Working Directory

git status is a vital command that provides a comprehensive overview of the current state of your Git repository. It tells you:

  • Your current branch: Which branch you are actively working on.
  • Staged changes: Files that are ready to be committed.
  • Unstaged changes: Modifications you've made that are not yet marked for commit.
  • Untracked files: New files that Git is not yet tracking.
  • Whether your branch is up-to-date with its remote counterpart.

When to use it: Frequently! Before committing, after pulling, or anytime you want to understand what's changed and what needs to be done.

How to use it:

git status

Example Output:

On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   index.html

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   styles.css

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        new_feature.js

This output clearly shows you're on the main branch, have index.html staged for the next commit, styles.css modified but not yet staged, and a new new_feature.js file that Git isn't tracking yet.


Meow! AI Assistance Note

This post was created with the assistance of Gemini AI and ChatGPT.
It is shared for informational purposes only and is not intended to mislead, cause harm, or misrepresent facts. While efforts have been made to ensure accuracy, readers are encouraged to verify information independently. Portions of the content may not be entirely original.

image
Photo by Yibo Wei on Unsplash