How to use handy git tools?

| | 3 min read

Git is a version control system which is distributed and is famous for its speed and efficiency. Git is different from its predecessors in so many ways, it handles the data as snapshots rather than file differences. Also almost all the operations in git is done at the local repository thus called distributed. Since all the operations are local it gives the power of speed to the end user.However more than this git also provides some tools which makes the git usage more easier and also increases the user friendliness.

Let us see some tools provided by git.

  1. Git stashing

    You may come across different situations where you have untracked files in your current branch and you have to do some other work in another branch. Since there is untracked files, you can't move to the other branch. Usually we will checkout these changes by copying the files to some temporary location and then move to the other branch. But you can use the git tool "git stash" here. When you try to checkout to other branches a git message itself is displayed which indicates that "You have to either stash or commit the changes you have made".

    So git stash will stash the changes made in the current branch. This is like saving the changes to a stack. We can then change to any branches and do whatever we want and come back to this branch and regain our changes. This is most helpful if we work in multiple tasks(branches) at the same time. The major options with this tool are,

    1. git stash

      This will stash the current changes which can be used later. You can also use git stash save "message" to stash the changes with a message to identify the changes easier.

    2. git stash list

      This will list all the stash points you have saved in different branches.

    3. git stash apply

      This will apply the top stashed point on the array, ie the last changes you have stashed. You can specify a particular stash by specifying the name of that instance.

    4. git stash pop

      As the name implies this will pop out the top stashed point from the array and apply it to the current branch. It will no longer be available in the stash array of elements.

    5. git stash drop

      This can be used to drop a particular stashed instance, you can specify which one to be dropped by specifying the name of the stashed instance.

    6. git stash branch

      You can create a new branch from a stashed instance which can be used for merging with other branches or resolving any merge conflicts with the modified content of the same branch.

  2. Changing the last commit

    This will be most wanted tweak at all times. You have committed with a wrong commit message, so you need to change the last commit message or you need to change the snapshot just committed. If you only need to change the last commit message, that is very simple. You only have t use,

    git commit --amend

    But if you need to change the last recorded snapshot, ie you need t change the files that you have commited or you have add new files or remove existing one. For this you only need to use git add <file> or git rm to stage the changes and the git commit --amend takes the current staging area and record it as your new commit. Do not change the last commit if you have already pushed it.

  3. Delete a branch

    There may be situations when you need to delete a branch on local or on the server. Somehow you have committed wrong files or there were a hilarious mistake you made. If you need to delete a local branch you can do,

    git branch -d <local-branchname>

    Sometimes you need to delete a branch on the server, may be you have pushed wrong content to a branch with the ticket name, so you can only have one branch with the same ticket number and also you have to reduce the number of commits to a minimum. Then you can delete the remote branch using,

    git push repo :remote-branchname

These are some tools that can be used to improve our git experience. There are many other tools available, try them and make the git usage more easier.