Git Commands that you must know

Hello fellow developers, have you ever come across a situtation where you needed to learn about git commands but didn't know where to learn from and felt like this not my cup of tea. Good news for you have reached the right place. Get ready to dive into the world of git with me.

All those hours of scrolling and getting frustrated over to find the right git command has come to end. I will be explain some of the most needed and commonly used git commands in today's article to make you live easy as a developer.

What is Git and Why is it important?

Git is a version control system, It allow's multiple developers to work on the same codebase in an efficent way by maintaining record about each change with details like change done by whom and easy revert features.

Imagine you're working on a software project with a team of developers. Each developer is responsible for writing code for a specific feature or fixing bugs in the codebase. Without a version control system like Git, coordinating these efforts can be chaotic. Developers might overwrite each other's changes, lose track of who did what, or struggle to merge their code together.

How Git solves these problems:

  • Version Control

  • Collaboration

  • Branching and Merging

  • Backup and Recovery

  • Code Review

Some Git commands

Before digging into the commands lets get famililar with some git terminologies.

  • Commit: A Git commit is a log or an entry of changes to the codebase, recorded with a descriptive message that is added when developer commits a change.

  • Repository: A Git Repository or repo is a codebase that contains all the code files of a particular project.

  • Push: A git push is when you commited you changes with an appropriate message and send to the remote repository on a version control system like github/gitlab.

  • Pull: A git pull is when your fellow developer has commited and pushed a changed to the remote repository and you want those changes on your local repository you can use git pull for such cases.

  • Branch: A branch is an isolated copy of the main branch, developers use branches soo as to avoid making changes on the main production code to avoid unnecessary errors and merge to main branch upon successful development.

Let's understand git commands by seeing how a repo is made and how it is used.

  1. Initially we initialize a git repo, it is same has creating a repo in your local machine. Go to the folder you want to make as a repo and enter the following command in the terminal.

    git init

  2. Next we add a remote url to the local repo soo as to connect the local and remote repos to know where to push and pull from.

    git remote add origin https://github.com/username/repo-name

    origin here can any variable name

  3. We make a branch to push our changes to. main is the name of the branch. Name of the branch can be anything but must be unique in a repository.

    git branch -m main

  4. Next we stage our changes we can do this for each file or the whole folder as once using . operator.

    git add . for all the files in the folder

    git add filename for an individual file

    Staging is used to keep track of files that need to be committed, only these will be pushed.

  5. Next we commit the staged files in the local repository.

    git commit -m "commit message"

  6. Next we push the commited change.

    git push -u origin branch-name

    origin is the variable we gave to the remote url and main is the branch name.

  7. Now suppose we pushed a change, but caused a problem and we want to go back to the previous code we can do this easily in git using git reset.

    git reset head~1

    1 means reset only 1 commit, we can specify more if we want. head will always be the latest commit by default and can be reseted. The changes can be pushed to remote repository using git push -f origin branch-name -f meaning force update since its reset.

    Note: use this carefully as it cannot be reversed.

  8. To get the changes from the remote url made by other developers we can use git pull origin branch-name .

  9. To switch branches locally we can use git checkout branch-name .

  10. If we want to merge branches we can use the command by staying the branch we want to be merged. for eg: merge main to master

    git checkout master

    git merge main Merging master to main

    git push -u origin master push to the remote repository.

  11. Another useful command is the rebase command, it is quite useful and used alot in tech companies.

    git rebase main it is use streamline project and reduce inconsistent commit history's. To learn more click here.

Bonus command

The command I used the most based on my experience is the stash command. It helps me when i make a mistake locally and want to restart making the changes. it will reset your code to the version in the latest commit.

git add . stage the changes

Git stash removes the changes using stash command(Although these changes are not lost and can be applied again using git stash pop)

Adios mon amigo

I hope you guys have learned some of the commands. Finally ending with a gitastic joke Why did the programmer break up with Git? Because they couldn't commit to a single branch!

Note: for any corrections do reach out to me using my socials present in the navbar.

3
Subscribe to my newsletter

Read articles from Aaron Jevil Nazareth directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aaron Jevil Nazareth
Aaron Jevil Nazareth

I am developer from India, Developed multiple websites using nextjs and typescript. Currently exploring the machine learning field . Would love to learn as much as possible, Also take up some freelancing gigs occasionally.