10 Must-Know Git Commands That Will Save You Hours

Git is an essential tool for developers, but without a solid grasp of its core commands, you can easily find yourself stuck, wasting…

10 Must-Know Git Commands That Will Save You Hours
Photo by Roman Synkevych on Unsplash

Git is an essential tool for developers, but without a solid grasp of its core commands, you can easily find yourself stuck, wasting precious time. Whether you’re just starting with Git or looking to refine your workflow, mastering these ten commands will boost your efficiency and save you hours of troubleshooting.

1. git init – Initialize a New Repository

When starting a new project, you need to set up a Git repository. The git init command creates a new repository in your project directory.

Usage:

git init

This command initializes a new Git repository (basically create a .git file) in your current directory, allowing you to start tracking changes. If you’re cloning an existing repo, you don’t need to run this command.

2. git clone – Copy an Existing Repository

If you want to work on an existing project, you can clone the repository using git clone.

Usage:

git clone <repository-url>

Example:

git clone https://github.com/django/django.git

This command creates a local copy of the repository on your machine, including all branches, commit history, and files.

3. git status – Check the State of Your Repository

Before making changes, it’s always a good practice to check the status of your repository.

Usage:

git status

It shows you:

  • Modified files that need to be committed
  • Untracked files
  • The current branch you are working on

This command helps prevent unnecessary mistakes by giving you a clear picture of your working directory.

4. git add – Stage Changes for Commit

Before committing your changes, you must stage them using git add.

Usage:

git add <file-name>

To stage all changes:

git add .

This command moves your modified files to the staging area, preparing them for commit.

5. git commit -m "message" – Save Changes

Once your changes are staged, use git commit to save them.

Usage:

git commit -m "Descriptive commit message"

Example:

git commit -m "Fixed login bug"

Each commit acts as a snapshot of your project at a specific point in time, making it easier to track changes.

6. git log – View Commit History

To see all previous commits and their details, use:

Usage:

git log

This command displays:

  • Commit hashes
  • Author names
  • Timestamps
  • Commit messages

For a more concise, one-line view, use:

git log --oneline

7. git checkout – Switch Branches or Restore Files

You can use git checkout to switch branches or restore a previous version of a file.

Usage:

git checkout <branch-name>

Example:

git checkout feature-branch

To restore a file to its previous committed state:

git checkout -- <file-name>

This command is useful when you make changes and want to discard them quickly.

8. git branch – Manage Branches

Branches allow you to work on features independently.

To list all branches:

git branch

To create a new branch:

git branch <new-branch-name>

To delete a branch:

git branch -d <branch-name>

Using branches effectively helps in managing different features and avoiding conflicts in collaborative projects.

9. git pull – Update Your Local Repository

To fetch the latest changes from a remote repository, use:

Usage:

git pull origin <branch-name>

Example:

git pull origin main

This command downloads the latest commits and merges them into your current branch, ensuring your local copy is up to date.

10. git push – Upload Changes to a Remote Repository

Once your changes are committed, push them to a remote repository using:

Usage:

git push origin <branch-name>

Example:

git push origin main

This command uploads your local commits to the remote repository, making them available to other team members.

Final Thoughts

Mastering these essential Git commands will streamline your workflow and save you from unnecessary frustration. Whether you’re a beginner or an experienced developer, knowing how to navigate Git efficiently will make a huge difference in your productivity.

Do you have a favorite Git command that has saved you time? Share it in the comments!