Skip to main content

Command Palette

Search for a command to run...

Git Commands every Developer Should Know!

Published
6 min read
Git Commands every Developer Should Know!
N

Writing Code

You guys must have installed updates for your favourite apps like Instagram,Facebook etc. Were you at any point got frustrated by some feature and wanted to go back to the previous version of the app? While this may/ may not be possible for an end user, but for an application developer, it is easy. We have what are known as "Version Control Systems" which help us track versions of different projects.

Git is one such very popular version controlling system. And you would be surprised to know that it was built by the guy who is a father figure in tech- Linus Torvalds. He built Git, a software used by millions of users today, in just 5 working days. I was shocked and motivated at the same time

What does Git do?

1-Track history; When we want to add features to existing product, we want a software to save its version history and track what changes were made each time we added something. Git helps us do this

2-Collaboration- In a big project,several developers collaborate on the same code, but this could lead to chaos as many developers can make changes to same piece of code. Git provides a solution for this as well. It keeps a track of which developer made what changes.

But what is GitHub then?

GitHub is a website that allows us to manage code using git. Imagine GitHub being the IDE and git being any programming language like c++, java anything. GitHub is providing us a means to use git to manage projects and codes.

When we navigate to GitHub,we find a section of repositories. Repository is basically a folder where we will be storing our code

After knowing this much of basics, let us move to our commands

Git commands

Git config

Configuring your git locally for your system, download git on your machine and run

git config --global user.name "your name/github profile name"
git config --global user.email "your email"

Once you have configured git to your machine, you are ready to run it.

Clone and Status

/*cloning basically means to pull code from 
any remote repo to our local machine *.
In the terminal write

git clone "link to the github code"

Running this command will bring the code from anywhere in GitHub to our machine. This is the most basic step to know if we want to contribute to any open source code base.

If you hit the command ls -a in your terminal, it will give you a list of all the files, including the hidden ones. One such file is a .git directory which is used by git to track changes.

/* Git status command tells us the status of the changes in files, which we made.
git status
it will tell us that in which files we made changes, and will ask us to commit.

Git add ,Git commit and Push

Committing or making a change in a file is a 2 stage process.

1- git add .- it adds all the changes which were made in order to commit

2-git commit -m"commit message"- it commits all changes with a message.

Note that this will not change the files in our github code.

To push the changes we use the

3-git push origin main- to push the changes directly into the main branch

Git Init

Sometimes when we want to create a new git repository directly from our IDE, we use git init command. It creates a .git directory which will track for changes.

Using the same commands as above we can track changes. But this will be added in our local system. To upload these on GitHub, we create a new repo.

Then we run the command git remote add origin <-link->

Git Branches

When different people are working on separate features, it is important to make the changes separate. This can be achieved by using Git branches. We can make changes on different branches and finally merge all of them into one

Creating a new branch- this can be achieved by the command;-

git checkout -b <branch name>

to move from one git branch to other, we write;-

git checkout <branch name>

Now we will do changes inside specific branches

we will push the code to github by writing git push origin <branch name>

To merge the branches, simply write

git merge <main branch with which you want to merge>

Or we can create a PR

PULL REQUESTS-

A pull request is a method for submitting contributions to github.

Think of it as a request to the maintainer of the project to merge/pull your changes.We can create a pull requests using the github website, and it will send a pull request to the maintainer, which will allow the maintainer to check for the changes and merge the pull requests

How to Create a Pull Request (Assuming GitHub):

These days making open source contributions can upskill you in many ways. For that we should know how to create a pull request

  1. Fork the Repository:

    • On the project's GitHub page, click the "Fork" button. This creates a personal copy (fork) of the project in your GitHub account.
  2. Clone Your Fork:

    • Clone the forked repository to your local machine using git clone.
    git clone https://github.com/your-username/repo.git
    cd repo
  1. Create a Branch:

    • Create a new branch for your changes. This keeps your main branch clean and allows you to work on multiple features independently.
    git checkout -b feature-branch
  1. Make Changes:

    • Make the necessary code changes in your branch.
  2. Commit Changes:

    • Commit your changes with git commit.

    •   git add .
        git commit -m "Description of changes"
      
  1. Push to Your Fork:

    • Push your changes to your fork on GitHub.
    git push origin feature-branch
  1. Create Pull Request:

    • On GitHub, navigate to your fork and branch. Click the "New Pull Request" button.

    • Select the base repository (the project you're contributing to) and the base branch (usually "main" or "master").

    • Select your fork and branch as the compare branch.

    • Add a title and description for your pull request.

  1. Merge the Pull Request:

    • Once the changes are approved, a maintainer can merge your pull request into the main branch.

Git Conflicts

Alright, imagine you and your friend are both working on a school project, and you're editing the same document. Now, let's say you both try to merge your changes back into the main document at the same time. That's when a "merge conflict" happens.

In simpler terms, a merge conflict in Git is like when you and your friend change the same part of the project, and Git doesn't know which change to keep. It's a bit like having two different answers to the same question.

To fix a merge conflict, you review both sets of changes, decide what should stay, what should go, and then tell Git what to do.

It is usually done by maintainers of the projects!

Undoing Changes

Changes which are staged(added but not committed )- git reset

changes committed- git reset --hard

this will undo the changes we committed, even locally

This marks the end of this git blog. Let us try to use this knowledge of git to make open source contributions and upskill ourselves.!