Git

Categories: Tech

Basic

Git is a version control system for software projects. In this post I will list down some of the important commands of git and their usage.

Branching

Merging

Git performs merging in two ways: 1st forward merge and 3 way merge.

When there is a linear path from feature to master branch, the master branch pointer just has to be translated down the straight path to the feature pointer. This kind of merging is called 1st forward merging. 1st forward merging do not create new auto-commits by git before merging.

In case the master and feature branch takes different path after a commit point in the past, Git looks at 3 different points when a merge command is executed. 1) The tip of the feature branch, 2) The tip of the master branch, 3) The commit point from where the feature and master branch diverged. Based on this 3 points, git combines the changes by creating new commit called the Merge Commit, then merges the two branches.

Rebasing and Squashing

When you want to keep your history of your public branches very clean, you do not want to see all the merge commits which was made by git in the process of merging. So you can do “squash and merge” or “rebase and merge” In Squash and Merge, commits will be combined to one commit in the base branch. The final commit message in the master branch will contain the commit messages of all the commits which will get combined to one. In Rebase and Merge the commits will be rebased and added to the base branch. Rebasing is essential when you want to take only a subset of commits from a branch and merge it into another branch. Rebasing puts all commits from a specific feature branch and puts them on the top of main base branch. Doing these in local repo is a multistep process. First rebasing and squashing.

Take the SHA1 of last commit before creation of a feature branch, git rebase -i SHA1 will open rebase in an interactive mode. You need to choose options what you want. It will open a text file where you will have to choose options before your commits regarding how do you want to rebase them. Eg. s 2d458754 File 4 was created in the feature2 branch, or, pick 2d458754 File 4 was created in the feature2 branch. Then close the file and you will be taken into another text file which is the final commit message file. Once you edit and close it, git will rebase as per your mentioned options.

Sharing & Updating Projects

Logging

Reset and Revert [DESTRUCTIVE, CAUTION!]

Amend

git commit --amend command is a convenient way to modify info into the very last commit.

Cherry-pick

git cherry-pick copy one commit from one branch to another branch

Reflog

git reflog can be useful to log all operations made in your LOCAL repo. It outputs every operation with its SHA1 You can copy this SHA1 and use it to checkout a specific commit. Eg, git checkout 0a681442

Stashing

Garbage collections (gc)

This is very well explained here: Atlassian website :

Git repositories accumulate various types of garbage. One type of Git garbage is orphaned or inaccessible commits. Git commits can become inaccessible when performing history altering commands like git resets or git rebase. In an effort to preserve history and avoid data loss Git will not delete detached commits. A detached commit can still be checked out, cherry picked, and examined through the git log. In addition to detached commit clean up, git gc will also perform compression on stored Git Objects, freeing up precious disk space. When Git identifies a group of similar objects it will compress them into a ‘pack’. Packs are like zip files of Git bjects and live in the ./git/objects/pack directory within a repository.