Let’s now dive a little deeper into Git!

Part 2


The git commands you know by now are as follows:

Sr. No. Git Commands Descriptions
1. git --version To check the version of the Git installed
2. git config --global user.name "Ash" Configures your name with Git
3. git config --global user.email "[email protected]" Configures your email with Git
4. git init Initializes or reinitializes a repository in a directory
5. git status Shows you the status of your current repository
6. git add file1 file 2 Adds specific changes made to file1 and file2 to the staging area, waiting to be committed
7. git commit -m "write a message" Commits your staged changes to the .git repo
8. git add. Adds all the changes you made in your directory to the staging area

Now, Git works in a tree like structure. The main branch is the trunk of the tree, from which, new branches will come out and they will have branches of their own. This is a very important and key feature of git that makes it so powerful. Consider the following diagram.

The center line highlighted in green in your main/master branch. And the one that bifurcates above and below are highlighted in red, which are your normal branches.

When you create a git repository, you will be by default on the Master branch. In terms of functionality, there is no difference between the master branch and other branches. If you want to see which branch you are currently on and other informations, run the following commands:

Sr. No. Git Commands Descriptions
1. git branch Tells you on which branch you are currently on
2. git branch *branchname* Creates a branch with the BranchName that you specify
3. git switch *branchname* You can switch from the main branch to the BranchName you specify
4. git switch -c *branchname* Create and simultaneously switch to the BranchName you specify
5. git branch -D *branchname* Deletes a branch with the specified BranchName
6. git branch -m *branchname* Renames a branch as specified by the BranchName

Note: You cannot Delete a branch while you are on it, you must first switch to another branch, and then try deleting it. Also, if you wish to rename a branch, you must be on it!

Merging

This is a way to merge the branches back to the main branch. Why would you want to do it? Well, in cases where you liked something you did on a branch and you want to incorporate or included those changes to the work you have done on the main branch, you do Merging! For example, you wrote a few chapters for your novel and committed everything to the main branch as a final draft. Then you decided to add some art work to your novel to make it more appealing. You created a branch called art. You liked how it turned out and now you want to incorporate those arts into your main novel (main branch). You do merging.

Now there are scenarios where there will be merge conflicts. A merge conflict is when the changes in the master branch and the changes in the other branch are conflicting in nature. For example, suppose you deleted the first chapter in your master branch, and added the same first chapter in your art branch and tried to merge them together. The git gets confused as to whether it should keep the first chapter or not. This is the simplest scenario, there can be complicated conflicts that arise as well, which you will learn from experience.

How to solve a conflict?

  • When there is a conflict while merging, you will get an error message.
  • Understand the error message and go to the files that caused it to make changes that can resolve the conflict.
  • After making the necessary changes, add them to the staging area and commit them.
  • Then Merge! In our case, you can resolve the conflict by making a decision to wither keep the first chapter or to discard it. Lets say you discard it. You make this change, and then add this change to the staging area, and then commit it to the git repo. After this, attempt merging again, you will see no conflict.

Stashing

Now, if you are confused about the art work that you made for your novel, you might decide not to commit those changes. What if you want to switch from art branch to the main branch and continue working on your draft. In this case git will give you a message that you must commit the changes in your art branch before switching to another branch. What will you do? When you don’t want to commit changes to your current branch and what to switch to another branch to do something else, you stash your uncommitted changes! Use the following command to do so.

git stash

Run the following command to remove the recently stashed changes and re-attach them to your working-copy.

git stash pop

The following are some of the commonly used stash commands:

Sr. No. Git Commands Descriptions
1. git stash list Returns a list of all the changes you stashed in the stash stack
2. git stash apply stash@{3} To re-attach and apply a particular change out of the list of stashes in your stash stack
3. git stash drop stash@{1} To delete a particular change from the stash stack
4. git stash clear Deleted everything in the stash stack

There are many other concepts like Diffing, checkout commands etc. which you can explore.

Cloning

Cloning is basically having all the files/history/commits of someone else’s repo to your machine. Fir example, you liked someones work on the cloud and you want to go through it on your local machine. All you need is the url of that repo and clone it by running the following command

git clone url

In the next blog, we will talk more about GitHub and managing your work on it.