Important Git Commands & Concepts

Important Git Commands & Concepts

ยท

6 min read

If you are interested in open source, it's crucial to learn Git. In this article, I'll cover essential Git concepts and commands. While you may be familiar with some of them, understanding how to apply them in specific scenarios is critical.

First of all, let's get familiar with some basic commands:

Basic Git commands ๐Ÿ‘ฉโ€๐Ÿ’ป

  • git init - initializes git repository in your folder

  • git add <file> - stage changes to commit

  • git commit -m "..." - commit changes to the current branch with a message

  • git restore <file> - discards changes completely

  • git push <remote> <branch> - push changes from local repo to remote repo

  • git pull <remote> <branch> - pull changes from the remote repo to the local repo (combination of 2 commands: fetch and merge)

  • git diff - compare changes with another branch or repo

  • git fetch <remote> - fetches changes from remote repo

  • git merge <branch> - merge changes of the given branch to the current branch

  • git branch - to check local branches available

  • git branch <branch> - creates a new branch with the name given

  • git checkout <branch> - switches to the given branch

  • git log - to see the commit history

  • git revert <commit> - reverts changes of that commit by making a new commit. Log history is unchanged.

  • git reset <commit> - works similarly to revert but will cause a change in the commit log. Must be used cautiously.

  • git commit --amend -m "..." - amends message for the previous commit

  • git remote - gives details of remote repos connected to your local repo

Now let's dive deeper into some important concepts.


Merge Conflicts ๐ŸฅŠ

This is the most common problem you will come across when contributing to any open-source repository. While you are working on your branch many other contributors will be also suggesting changes to the source code. Hence you should keep your branch up to date with the master but if you don't then you might get into this issue. In general, we can say that;

"Merge conflict happens when two or more people are working on the same codebase simultaneously and make changes to the same part of the code. When they try to merge their changes back into the main branch or repository, the version control system detects that the changes conflict with each other and cannot be automatically resolved."

Let's create a merge conflict and try to resolve it:

First of all, create a branch out of the master branch by running the following command in the terminal:

git checkout -b add-list

Make some changes to any file and commit those changes to this branch. Now let's assume there was an emergency and you have to fix some code in the master branch. So you do the following:

# goes back to previous branch, in this case master
git checkout - 
# create new branch bug-fix 
git checkout -b bug-fix
# make changes and commit them to bug-fix branch
git commit -am "fixed ..."
# checkout to master branch
git checkout -
# merge changes
git merge bug-fix

Now at this point, your "add-list" branch is unaware of the new changes. Moreover, if it has made changes to the same file as the "bug-fix" branch, it would lead to a merge conflict. So try to run the command (make sure you are on the master branch): "git merge add-list" and you will get something like the below:

To resolve this you have to decide on the final changes that you want to keep and then commit those changes again. This will resolve the conflict and complete the merge operation. Now you can safely delete the other branches.


Revert ๐Ÿ†š Reset

The revert command as I said before is used to undo changes without rewriting project history. Whereas reset is used when you don't care about the commit log being changed. In general, revert is a safer option, while reset should be used with caution and only when you're sure that you want to permanently discard changes or rewrite the project history.

Let's take an example:

You have accidentally deleted a file and committed the changes as well. You want to undo it using revert so you run the following command.

# this will undo the latest commit with a new commit and default message
git revert HEAD --no-edit

Now let's try to reset to an old commit by running the following command:

git reset <commit hash>


Merge commit, Squash, or Rebase โ‰

You must have heard these terms frequently but are confused as to which is better, right? Well each of them works differently and should be used according to the scenario.

Merge commit - It will merge all incoming changes along with a new commit. This should be used when you want to keep the branch's history preserved.

When you run git merge in the terminal it will not necessarily do a merge commit i.e if the changes can be fast-forwarded (adding changes to the master branch as if we worked on it, linearity is observed) it will simply merge those changes without making a new commit. If there's no linear history anymore then in that case you have to do a merge commit.

Squash - This is used when you want to merge all the changes to the target branch without having to keep the commit history of the feature branch. We just do a single commit for all those changes. Very useful if your commit history is not of importance.

Rebase - Git rebase is another way to integrate changes from one branch into another branch. Instead of creating a new commit that merges the changes, rebase moves the entire branch to a new base commit, incorporating the changes from the other branch as if they were made on top of the new base commit. Essentially, the rebase rewrites the project history to incorporate the changes more linearly.

You can look at the image below to better understand the difference between "rebase" and "merge";


Some other useful commands that I forgot to mention are:

  • git log --oneline - a shorter summary of the commit log

  • git log --graph - to get a visual representation of commit history

  • git checkout - - to switch back to the previous commit

  • git checkout -b <branch> - switches to a branch and creates it if not already present.

  • git remote set-url <remote name> <remote url> - changes url for existing remote

  • git remote add <remote name> <remote url> - creates a new remote

Great! You have now got familiar with some important Git commands and also learned some useful concepts. You can practice Git commands now and start contributing to your favorite open-source projects. Happy coding ;) ๐Ÿ’—

ย