logo logo

10 More Useful Git Commands You Should Know

10 More Useful Git Commands You Should Know

This is a follow-up to the 10 common Git commands article that I wrote a while back and contains some other useful commands that can help in our work. If you understand the previous 10 commands, let’s go over some more 😊

1. git status

Git status without unstaged changes

It will also show me if some of my changes are not staged (so they will not be included in the commit):

Git status with unstaged changes

Or let me know when there are no changes:

Git status with no changes

2. git rm

rm is short for “remove”. We use this command when we want to delete a file from the solution ❌

Useful Git commands: git rm

3. git mv

Stands for “move”. We can use it to rename files inside the project or move them to a different location.

Here’s an example of a file, and “git status” reflecting the change:

Git mv to rename a file

And here’s an example of a file moved to a different directory. Please keep in mind that Git will not see this change as a move, but as a rename, even if the file name remains the same 🧐:

Git mv

4. git log

Another one of the very useful Git commands is “git log”. This command will show all the commits on the branch, starting from the initial commit on the master branch, and continuing with all newer commits:

Useful Git Commands: git log

You can see the author, the date and the time, and the comment for each commit πŸ’»

5. git revert

If you want to undo a previous commit, this is the command you need to use. If you are unsure which is the commit to revert, use the previously explained “git log” command to get all the previous commits info:

Useful Git commands: git revert

This will undo all the changes from the previous commit. In this example, only one file had been changed. Used like this, the revert command will automatically commit these changes.

If you don’t want to commit the changes, you can add the –no-commit option:

Git revert no-commit

You’ll see that if you use the “git status” command, you’ll have changes that are not yet committed. This way you can review the changes and make additional ones (or revert some of them) before committing πŸ’ͺ

6. git show

This command shows more information about a particular commit. You need to send the command using the commit. The output will look like this:

Git show

7. git tag

Tags in Git allow you to “label” the commits. For example, you may want to add tags related to specific versions of the applications (something like “v.0.1.”) πŸ”–

To add a new tag, simply run the command:

git add <tag_name>

Then, you can list all your tags with:

Create new tags

By default, the tags are created on the commit referenced by HEAD. To add a tag to a different commit, get the commit SHA using the “git log” command shown above, and use the command:

git tag -a <tag_name> <commit>

You can also delete tags you no longer need by using:

git tag -d <tag_name>

You can read more about tagging in Git in their documentation here.

8. git stash

Another one of the very useful commands. Git stash allows you to locally save your tracked changes, without committing them. This way you can revert unstashed changes, or you can move to another branch and apply these stashed changes to it πŸ”₯

Here’s how it works:

Useful Git commands: git stash

  • git stash save“: Creates a new stash with the changes and reverts the branch to the last commit.
  • git stash list“: Displays all available stashes. I created two in the example to illustrate how this would look.
  • git stash drop“: Deletes the latest stash.
  • git stash pop“: Applies the changes to the branch.

9. git merge

Git merge is used when you want to bring the changes from another branch to the current branch:

Git merge

10. git reset

This command is useful for undoing changes that were already committed in the previous commit.Β  There are two ways to undo these changes: “hard” (which deletes all changes and reverts the branch to the previous commit), or “soft” (which will undo the commit but keep the changes):

git reset --soft HEAD^
git reset --hard HEAD^

Final thoughts

Once you start using Git, you can see that it can perform quite advanced actions. What I find really useful is that we can use Git commands to double-check our changes before committing them, or even undo them in case we made any mistakes πŸ˜…

Did you enjoy part two of useful Git commands?
Share in the comments if you are interested in learning more and which command was the most useful.

About the author

Andreea Draniceanu

Andreea is a QA engineer, experienced in web and desktop applications, and always looking to improve her automation skills and knowledge. Her current focus is automation testing with C#.

Leave a Reply

FacebookLinkedInTwitterEmail