Git  Commands you may not know but can be very useful

Git Commands you may not know but can be very useful

·

4 min read

Git commands you may never know but can be very useful in certain situations.

First let's discuss in short what is git.

What is Git?

Git is a free and open-source Distributed Version Control system. Real-life projects have multiple developers working in parallel. So they need a version control system like Git to ensure that there are no code clashes between them. Before Git, developers were using CVS and SVN version control and they were famous too. But why they are not using it now and using Git? Because Git uses something like a staging area, this created a revolutionary version control system.

Git Commands

Modify the most Recent Commit

git commit --amend -m "message"

This command can be handy if you have done some typo mistake in your message while committing or some minor bug that you remember after you committed. The --amed tag can help you to modify the commit. While committing it lets you combine staged changes with the previous commit instead of creating an entirely new commit. To Git it looks like a brand new commit which is visualized with an asterisk sign in the diagram below.

rewriting_history_001 (1).png

Please make sure to use this command properly or else you will lose your data.

Interactively add selected parts of files to the staging area

git add -p

This command basically means **git add partial (or match)**. This allows you to stage parts of a changed file instead of an entire file. This feature can improve the quality of the commits. At each point, you need to select which hunk you need to stage. You will be needing the below commands to add or remove the hunks.

  • y - stage this hunk
  • n - do not stage this hunk
  • a - stage this and all the remaining hunks in the file
  • d - do not stage this hunk nor any of the remaining hunks in the file

add-p.PNG

Check out for more

Interactively stash selected parts of the file

git stash -p

OR

git stash --patch

This command will not stash everything that is modified but will instead prompt you interactively on which of the changes you would like to stash and which you would like to keep in your working directory. Just like git add -p you can use their extra commands to stash hunks interactively in your file just like the below image.

add-p.PNG

Stash with Untracked files.

git stash -u

This command will help you to stash untracked file changes. You can use -u or --include-untracked to stash untracked files just like the below image.

stash-u.PNG

Git won't be stashing the ignored file that you have ignored explicitly in the .gitignore file.But it also stashes the staging area file.

Revert all the changes in the file.

git checkout --filename

This command will forget all the changes you made to a specific file and revert to its state beforehand before you commit. Check out the below image for reference.

checkout--filename.PNG

You should use it properly you can delete your changes forever.

Switch to the previous branch

git checkout -

As the name explains it will switch to the previous branch you were using. Check out the below image for Reference.

checkoutprevious.PNG

Revert all Local changes

git checkout .

This command will revert all your changes in your current directory and subdirectories. Check out the below image for Reference.

revertcheckout.PNG

Show Changes

git diff --staged

This will command will show you the staged changes difference with the commit like in the below image. Check out the below image for Reference.

diff-staged.PNG

Rename branches Locally

git branch -m old_name new_name

This command will help you to rename the branch that you have created. Check out the below image for Reference.

branchnamechange.PNG

There are many use cases for these commands and if you want I can cover more of these in the next part.

Want a Part 2 then like and comment Yes.