Git is one of the most popular version control tools out there. In this article, I will talk about few essential git commands which you must know. So, let’s get started!
Git add command is used to make the changes in the staging area. For example, I have added a new file “demo.txt” on my local system. If I run git status, you will see the file is currently untracked because this change has happened only locally.
$ git status On branch master No commits yet Untracked files: (use "git add
..." to include in what will be committed) demo.txt nothing added to commit but untracked files present (use "git add" to track)
Once you run the git add command, it will move the demo.txt file to the staging area. Now the file will wait in the staging area for a commit to happen.
$ git add demo.txt $ git status On branch master No commits yet Changes to be committed: (use "git rm --cached
..." to unstage) new file: demo.txt
If there are multiple files to add, you can run the command below to add all of them to the staging area.
$ git add -A
Git commit might be the most used command in git. This command is used to save all changes. For example, you have added a file demo.txt. Now, to save this change, you need to run git commit. You can use -m flag and write a short message to keep track of what this commit was about.
$ git commit -m "demo file added" [master (root-commit) c1c6a92] demo file added 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 demo.txt
Once the commit is done, if you run git status, it will tell you there is no pending commit which needs to be executed.
$ git status On branch master nothing to commit, working tree clean
In Git, rebase is a way of moving or combining the commits of one branch into another branch. As a developer, I would not create my features on the master branch in a real-world scenario. I would work on my own branch (let’s say ‘feature branch’), and when I have a few commits in my feature branch with the feature added, I would then like to move it to the master branch. This is where rebase is used. Consider, I have a graph which looks like this:
I will do a rebase, where I want to take my two feature branch commits and move them onto the master branch. A rebase will take all my commits from the feature branch and move them on top of the master branch commits. So, behind the scenes, git is duplicating the feature branch commits on the master branch.
Let me show you this practically.
This is how my master branch looks like currently. It has 4 commits.
$ git log --oneline 812335d (HEAD -> master) add 3 files again 0db602e one more commit 59c86c9 new commit e2f44fc (origin/master, origin/HEAD) test
I will run the below command to create and switch to a new branch called feature, and this branch will be created from the 2nd commit i.e. 59c86c9
(master) $ git checkout -b feature 59c86c9 Switched to a new branch 'feature'
If you check the log in the feature branch, it has only 2 commits, which is coming from the master (main line).
(feature) $ git log --oneline 59c86c9 (HEAD -> feature) new commit e2f44fc (origin/master, origin/HEAD) test'
I will create feature 1 and commit it to the feature branch.
(feature) $ touch feature1.txt (feature) $ git add . The file will have its original line endings in your working directory (feature) $ git commit -m 'feature 1' [feature c639e1b] feature 1 1 file changed, 1 insertion(+) create mode 100644 feature1.txt
I will create one more feature i.e., feature 2 in the feature branch and commit it.
(feature) $ touch feature2.txt (feature) $ git add . The file will have its original line endings in your working directory (feature) $ git commit -m 'feature 2' [feature 0f4db49] feature 2 1 file changed, 1 insertion(+) create mode 100644 feature2.txt
If you check the feature branch's log, it has 2 new commits that I executed above.
(feature) $ git log --oneline 0f4db49 (HEAD -> feature) feature 2 c639e1b feature 1 59c86c9 new commit e2f44fc (origin/master, origin/HEAD) test
I want to add these two new features to the master branch. For that, I will use the rebase command. From the feature branch, I will rebase against the master branch. What this will do is it will re-anchor my feature branch against the latest changes.
(feature) $ git rebase master Successfully rebased and updated refs/heads/feature.
I am going to go ahead and checkout the master branch.
(feature) $ git checkout master Switched to branch 'master' Your branch is ahead of 'origin/master' by 3 commits. (use "git push" to publish your local commits)
And finally, rebase the master branch against my feature branch. This will take those two new commits on my feature branch and replay them on top of my master branch.
(master) $ git rebase feature Successfully rebased and updated refs/heads/master.
Now, if I check the log on the master branch, I can see the two commits of my features branch have been added to my master branch successfully.
(master) $ git log --oneline 766c996 (HEAD -> master, feature) feature 2 c036a11 feature 1 812335d add 3 files again 0db602e one more commit 59c86c9 new commit e2f44fc (origin/master, origin/HEAD) test
Git rebase also has an interactive option that you can use with -i flag. This option is used to edit the list of commits before rebasing. For example, I want to edit the message of my last 3 commits. I will run an interactive rebase with HEAD~3.
$ git rebase -i HEAD~3
This will open an editor with last 3 commits. Now you can make your changes on those commits. Once you save and exit the editor, you will get the message of the changes.
Git Rebase vs Git Merge
Rebase can sometimes be a little confusing to understand because it is very similar to a merge. The goal of merging and rebasing both is to take the commits from my feature branch and put them on to a master branch or any other branch. The graph below shows the fundamental difference between merge and rebase in git.
In merging, you take the features branch content and integrate it with the master branch. And you add a new commit for this integration. Whereas in rebasing, you move the complete features branch to the endpoint of the master branch.
Amending commits in git is used often to correct mistakes. Since a developer would be adding multiple commits daily, there is a high chance he/she may add an incorrect message while committing. This is where --amend is used with git commit. Navigate to the commit you want to edit and use -–amend, which will open an editor to add the new message and save the commit.
If you want to edit the latest commit and pass the message from the command line, you use -m with --amend
commit --amend -m “commit log updated”
Different Ways of Viewing the Git Log
The log command in git is used to see all the details of the commit logs. By default, this command list all the commits in reverse chronological order. So, you will get the information for the latest commit at the top.
$ git log commit c1c6a923327150192f9cb37f8c4f7c214139a283 (HEAD -> master) Author: demo
Date: Thu Feb 11 01:03:21 2021 +0530 demo file added
You can also use --stat flag to see abbreviated stats of the commits. This will also list the modification (change/insertion/deletion) made in the commits.
$git log --stat commit c1c6a923327150192f9cb37f8c4f7c214139a283 (HEAD -> master) Author: demo
Date: Thu Feb 11 01:03:21 2021 +0530 demo file added demo.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-)
You can use --pretty flag for better formatting, other than the default option. When you have hundreds of commits to look at, you can use the online option with –pretty flag. It gives a single line output for each commit.
$ git log --pretty=oneline c1c6a923327150192f9cb37f8c4f7c214139a283 (HEAD -> master) demo file added
Reflog in git stands for reference log. Git keeps track of all the commits you make. reflog is a record which has details of all the commits made at any point of time. It is mostly used when you have messed up your commits. You can do git reflog and check the details of your past commits and use git hard reset to move to a stable commit.
$ git reflog c1c6a92 (HEAD -> master) HEAD@: commit (initial): demo file added
This post was all about git add, commit, rebase, log. Also, now you know the differences between rebasing and merging in git. So, go ahead and try out these commands yourself and reach out to if you have any questions.