Git Commands and Usage

Git Commit, Revert, and Diff: How to Use Them

See how git commit, revert, and diff fit together in a real Git workflow. Save changes, compare versions, and undo mistakes without fear.

Serhat ALPASLAN3 min read

Contents

using git and git tutorialsgitlab, github tutorials and git commands

Git is a popular tool used as a version control system. Git offers features you can use to store, modify and restore your files and code. In this article, we’ll look at some of Git’s most fundamental commands: commit, revert and diff.

Git Commit

This command lets you save the changes you make to your files. A commit stores the current state of your files and lets you restore them later.

//The command below lets us save the changes in the staging area
git commit

Git Push

This is the command that writes the changes you previously committed to the other side.

git push origin master

Git Pull

It merges the changes you’ve uploaded to the server into your local repository.

git pull

Git Revert

This command lets you undo a commit you made earlier. This lets you revert the changes you made to your files.

// you first need to run the code below to know the hash code you want to revert. You can find that out with the following code. In fact, you can think of this operation as git revert last commit.
git log

// We write the hash code we learned with the help of the code above as follows.
git revert hash_kodunuz

The example above performs a revert for a specific hash code, but in some cases you may need more than that.

Git Diff

This command lets you see the changes you made to your files. This command only shows the changes you made to your files and shows how they were changed.

git diff metin.txt

Git Reset

It will undo the changes you haven’t pushed yet. This operation is often confused with Git Revert, and I recommend using it carefully.

// The command below will let you return to the last commit you made or the last push you did.  git reset --hard HEAD

There are also things you need to know about Git reset: the –hard parameter will make the changes you made disappear. If you don’t want those changes to disappear, I recommend using the –soft parameter. Also, as I mentioned earlier, the command above will return you to the last commit or last push. If you want to go back a different way, that is, several commits back, you’ll need a usage like the following.

//The command below will let you return to the previous commit.
git reset --hard HEAD~1

//The command below will let you return to the commit before the previous one.
git reset --hard HEAD~2

These commands are among the fundamental commands that are used frequently when working with Git. By using these commands, Git offers features to store, modify and restore your files and code.

One of the most important things to pay attention to when using Git is to commit continuously. This lets you continuously track and restore the changes you make to your files. In addition, you can undo the mistakes you make with the Git Revert command.

I also recommend taking a look at the previously prepared git usage lessons. Also;

By examining these resources too, you’ll be able to understand what git is much better.

Share it if it helped

Write a comment