Everyday Git Commands: A Practical Guide

Everyday Git Commands: A Practical Guide

When I first started writing code, I was still a newbie and didn’t know much about version control systems or collaborative development tools. I used to transfer code between computers via USB drives, which was a tedious and error-prone process. This method made it difficult to track changes, collaborate with others, and maintain a consistent codebase.

Later on, one of my professors introduced me to the concept of Git, a distributed version control system that changed my approach to coding. Although Git wasn’t officially part of the course syllabus, my professor took the step to teach us its fundamentals. This introduction to Git was a turning point in my coding journey.

Git provided me with a structured and efficient way to manage code changes, track project history, and collaborate with other developers. It eliminated the need for USB transfers and made teamwork a seamless experience. I quickly realized that Git was a necessary tool for any aspiring programmer.

What is GIT?

Git is a free and open-source tool that helps you track changes in your code projects over time. It’s like having a detailed history of your project’s development, allowing you to revert to previous versions if needed. Git also makes it easy to collaborate with other developers on the same project.

Imagine you’re working on a big writing project with your friends. Git would be like having a shared notebook where everyone can write their contributions, and you can easily see who wrote what and when. It also lets you merge everyone’s work together without conflicts or confusion.

Git is widely used by programmers and software developers, but it’s also useful for anyone who wants to keep track of changes in their work. It’s a powerful tool that can save you time and frustration, especially when working on large or complex projects.

Git is a powerful tool for managing code changes and collaborating with other developers. It can seem daunting at first, but with a little practice, it becomes second nature. This guide will walk you through 90% of the daily Git commands you’ll need to know, with real-world examples.

Cloning the Repo

The git clone command creates a local copy of a remote Git repository. This allows you to work on the repository offline and then push your changes back to the remote repository when you're ready.

Here is an example of how to use the git clone command:

git clone <repository_url>

This will create a copy of the repository at the specified URL in the current directory.

Switching Between Branches

Git branches are a way of creating multiple versions of your code. This allows you to work on new features or bug fixes without affecting the main branch.

Branches let you work on multiple features at the same time without effecting your main branch code which is normally deployed.

Here is an example of how to use the git checkout command to switch branches:

git checkout <branch_name>

This will switch you to the specified branch.

Pulling the Changes

The git pull command fetches the latest changes from the remote repository and merges them into your local repository.

Here is an example of how to use the git pull command:

git pull origin <branch_name>

This will fetch the latest changes from the remote repository and merge them into your local branch_name branch.

Committing the Change

Git commits are snapshots of your code at a specific point in time. Commits are used to track changes to your code and to collaborate with other developers.

Commits are the most important part of Git. You can always revert to a specific commit at any point in time. Its recommended to make a commit whenever you complete a significant feature or reach a stable milestone. It will help you maintain a complete code history.

Here is an example of how to use the git add and git commit commands to commit changes to your local repository:

git add <file_name>
git commit -m "<commit_message>"

The git add the command adds the specified files to the staging area. The git commit command commits the staged files to the local repository with the specified commit message.

You cannot commit directly you first need to add those files to the staging area and then those files will be committed. It is like wrapping a gift, first, you need to put things in bo and then you wrap it.

Pushing the Change

The git push command sends your local changes to the remote repository.

Here is an example of how to use the git push command:

git push origin <branch_name>

This will push your local branch_name branch to the remote repository.

Managing the Stash

The stash is a temporary holding place for changes that you don’t want to commit yet. The stash is useful if you want to work on something else without committing your current changes.

Here is an example of how to use the git stash command to stash your changes:

git stash save

This will stash your current changes.

Here is an example of how to restore your changes from the stash:

git stash pop

This will restore your stashed changes. Pop will give you, your latest stash it works like an Array data structure if you are familiar with it.

Branching Out

The git checkout -b command creates a new branch and switches to it.

Here is an example of how to use the git checkout -b command to create a new branch:

git checkout -b <branch_name>

This will create a new branch named branch_name and switch to it.

Raising a PR

A pull request (PR) is a way of proposing changes to a codebase. When you create a PR, you are asking other developers to review your changes and merge them into the main branch if they approve.

Here is how to create a PR:

  • Create a new branch for your changes.

  • Make your changes to the branch.

  • Commit your changes and push your branch to the remote repository.

  • Open a PR on the remote repository. (you will find the PR option in GitHub or any other software you are using for remote repository)

  • Wait for other developers to review your changes.

  • If your changes are approved, merge your branch into the main branch.

Merging the Branches

The git merge command merges two branches together.

Here is an example of how to use the git merge command to merge one branch into another:

git merge <branch_name>

This will merge the branch_name branch into the current branch.

Rebasing the Branches

Rebasing is a way of rewriting the history of your branches. This can be useful if you want to clean up your commit history.

Here is an example of how to use the git rebase command to rebase one branch onto another:

Bash

git rebase <base_branch> <branch_name>

This will rebase the branch_name branch onto the base_branch branch.

The above two things are oversimplified here, and I know there’s a lot more to cover. I might write a blog about it in the future. For now, here’s a video that can provide some clarification.

ByteByteGo https://www.youtube.com/watch?v=0chZFIZLR_0&t=203s

Resolving the Conflict

If there are conflicts when merging or rebasing branches, you’ll need to resolve them manually. This is done by editing the conflicting files and then marking them as resolved. You can do it in Git Hub directly if there are minor issues you need to use a code editor.

Conclusion

While working with Git you will often find yourself writing the above commands that is why it is essential to at least understand the basic command and their usage in Git.

If you found this content helpful, please show your support by giving it a clap, following me on LinkedIn and YouTube, and considering making a small contribution to Buy Me a Coffee☕️. Your support is greatly appreciated!

Did you find this article valuable?

Support Muhammad Younus by becoming a sponsor. Any amount is appreciated!