6 git commands you should know

6 git commands you should know

I suggest you to consider the commands that will save you time and help you use the version control system more efficiently:

- reverting of the commit

- revert local changes

- comparing files and logs on different branches

- search for the commit that caused the error

- recovery of the deleted commit

- config individual git commands

Mirroring or Reverting

This command helps to undo previous changes. For example, you had a task to add 100 images to a project, you added them to a separate commit. Then you were told that you don't need them. The easiest way to undo commit changes is to use command revert. The only thing you need to know is the sha.

git revert <sha of commit>

The command creates a new commit by mirroring the changes.

For reverting the previous commit:

git revert HEAD


Revert local changes (not committed)

Command that will help undo all unnecessary changes.

Revert tracked changes:

git checkout .

git checkout config/routes.rb

Remove untracked files and folders:

git clean -fd

Difference between branches

The easiest way to compare changes on branches or changes in specific files:

git diff origin/master..HEAD


git diff origin/master..my_branch config/routes.rb

The difference in the history of commits between specific branches can be displayed by using the command:

git log origin/master..HEAD

git log origin/master..my_branch config/routes.rb

Search for erroneous changes - git bisect

This command helps to determine, which commit had introduced the error.

To start the search:

git bisect start

Then mark that your current commit has this bug:

git bisect bad

Then mark as good the commit where everything was working correctly.

git bisect good <sha of commit>

In the range bad..good commits command bisect switches you to the middle commit. Then you need to test and type good or bad commit now.

git bisect bad

or

git bisect good

After that, you switch to another commit from the middle not marked as good commits. Keep repeating the process: test and mark as bad or good the current one.

When you will see text “roughly 0 steps” that means you have only 2 commits, the current and the last one. When you mark the current one then you see the information about the commit that introduced a bug.

To return to the original HEAD, issue the command:

git bisect reset

if you specify sha, you can return to this commit instead:

git bisect reset <sha of commit>

Recover Deleted Commit

Rewriting the history is not a good idea, but if for some reason you removed the commit, and then realized that you want to restore it, review the reflog and find the sha of commit, then use the command - reset and push force.

git reflog

git reset --hard <sha of commit>

git push -f

Config your own commands

Git has many useful features that are in options, you can save the ones you use more often with your names using alias.

All your individual commands will be saved to your .gitconfig.

For instance:

git config --global alias.commend 'commit --amend --no-edit'

git commend

In conclusion: the best way to remember new commands is to practice and use them.