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
It will also show me if some of my changes are not staged (so they will not be included in the commit):
Or let me know when there are no changes:
2. git rm
rm is short for “remove”. We use this command when we want to delete a file from the solution ❌
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:
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 🧐:
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:
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:
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:
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:
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:
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:
- “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:
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^
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 👨💻