Git : A basic understanding for every Dev - Part IV

Git : A basic understanding for every Dev - Part IV

ยท

5 min read

Git Rebasing

In this section, we will take a look at git rebasing.

Git merge

  • We can merge the master branch into our own branch by the git merge command.

  • This creates a new merge commit, to merge these changes into our own branch. Another way to achieve this is by rebasing branches.

$ git merge master

Git rebase

  • Basically in the rebasing, we are putting a branch on top of another branch one.

  • After performing this, the current branch is containing all the changes that were made in the master branch.

$ git rebase master
  • In the GIT, each commit has a unique identifier. This unique identifier is a hash that contains the information about the commit.

  • When it's merged, this unique identifier won't be modified. In the other words, we are not modifying the history of the GIT commits. When we are merging.

  • When we are rebasing, actually we are copying commits from one branch to another branch. Each time a hash value will update.

Git Interactive Rebasing

In this section, we will take a look at git interactive-rebasing.

  • When all commits are looking the same and should be added into the single commit.

  • We can change the history of the git branch within the interactive rebase. To access this we use -i flag with git rebase command.

  • We have to specify which commits we want to update.

  • Suppose we want to modify the first four commits. We are telling git to rebase the last 4 commits.

$ git rebase -i HEAD~4
  • It will open the file into the editor, we need to change the "pick" to the "squash" command. After changing the commands save the file and exit.

  • After this, it will combine the commits. There are so many other options when you are interactively rebasing. It's a very powerful tool for changes to your branch and commits.

Git Cherry Picking

In this section, we will take a look at git cherry-pick command.

  • One branch has certain changes and you would like to apply to the other branch. But you don't want all the changes that you made in that branch. In certain things, we use cherry-pick command to copy that commit onto our branch.

  • We use the hash of the commit, that we want to use for a cherry-pick. After running this command, it will create a copy of the commit into the specified branch.

$ git cherry-pick aaba5

Git Resetting and Reverting

In this section, we will take a look at git reset and git revert command.

  • Everyone makes a mistake, in some situations we don't want to commit certain things but do. So there are several options to undo that commit.

  • One of the options is git revert command. The second one is git reset command.

Git revert

  • git revert , the command creates a new commit, which literally reverses the only changes that we made on the commit that we specified. A git revert command is useful if you want to undo the changes and keep those changes in your GIT history.
$ git revert <commit-id>

Git reset

  • In the git reset command, there are two ways to reset the commit. Either with the --soft flag which we want to keep the changes that we made or over the --hard flag to lose all the changes that we made on that commit.
# soft reset
$ git reset --soft HEAD~1

# hard reset
$ git reset --hard HEAD~1
  • When we reset the commit with --soft flag, we still have the access to changes that we made by that commit. We can see that status by the git status command. We can easily create another commit this way.
$ git reset --soft HEAD~1
$ git status
On branch newdemo
Changes to be committed:
  added:   demofile3.txt            # file shown with git status command
  • In the --hard flag, it will reset the commit without saving all those changes.
$ git reset --hard HEAD~1
$ git status
On branch newdemo
Nothing to commit

Git Stashing

In this section, we will take a look at the git stash command.

git stash

  • If we don't want to commit now, we can stash all changes in the working area with git stash command. Modification in the working area, all get added to the stash.
$ git stash
  • With git stash pop command, to get back changes in our working area.
$ git stash pop
  • To see the list of stash files, we can run the following command:
$ git stash list
  • To see the content of the specific stash file, we can run the following command:
$ git stash show stash@{1}
  • To pop the specific stash, we can run the following command:
$ git stash pop stash@{2}

Git Reflog

In this section, we will take a look at git reflog command.

  • If the commit is not necessary at all and wants to remove then we can run the git reset --hard command: -
$ git reset --hard HEAD~1
  • After running this command, that data will be gone forever. But we don't need to panic at all, git reflog command shows all actions that have been taken to the repository. This includes resets, reverts and merges.
$ git reflog
  • You can easily undo the mistakes you made in the repository of that information reflog command gives us.

  • You can get the hash value from the git reflog command as we already see in the previous process.

$ git reset --hard 8ad5
  • After this repository has been set into the previous state.

  • You can see that git reflog status also changed.

$ git reflog
  • git log and git reflog may look similar. git log will show you only information about the commits not the status of the repository that git reflog does.
$ git log

$ git reflog

Want to learn and explore more about git? Then do checkout the Git series

Hop into the blog section for a more interesting and detailed overview of Software development and other stuff. ๐Ÿ˜Š

Did you find this article valuable?

Support #DevOps Daily-Dose by becoming a sponsor. Any amount is appreciated!

ย