Hey everyone, in this tutorial, I’m going to show you how to use Git like a pro. We’ll go over about 10 commands that senior developers use all the time. You might already know some of these, but I recommend reading this article until the end because you’ll probably learn something new and useful.
Stage and Commit in One Command
Instead of using two separate commands to stage your modified files and then commit them, you can do it all in one go.
Normally, you would use git add . followed by git commit -m “message”. But you can combine these into a single, efficient command:
git commit -am "message"
This command stages all modified files and commits them with your message in one step.
Create and Switch to a New Branch
Here’s another great shortcut. To create a new branch and immediately switch to it, you can combine two commands into one. Instead of git branch <new-branch> and then git checkout <new-branch>, you can use:
git checkout -b <new-branch>
This single command creates the new branch and switches your working directory to it.
Rewrite the Most Recent Commit
If you need to change the most recent commit, you can use the amend command. This is useful for changing the commit message or adding more changes to the last commit. You can also use a shortcut to directly change the commit message in one line:
git commit --amend
git commit --amend -m "new message"
View a File from Another Branch
Ever wanted to see what a file looks like in another branch without having to switch branches? This command is incredibly useful. You can view the contents of a file from a different branch with:
git show <branch-name>:<file-name>
For example, to see the README.md file from the main branch, you would use:
git show main:README.md
Sort Branches by Commit Date
For developers working on large projects with many branches, this command is a lifesaver. By default, git branch lists branches in a random order. To sort them by the most recent commit date, use:
git branch --sort=-committerdate
You can even set this as your global default with:
git config --global branch.sort -committerdate
Undo a Commit While Keeping Changes
If you want to undo the last commit but keep all the changes in your working directory, this is the command for you.
git reset - soft HEAD~1
The git reset — soft HEAD~1 command will move the HEAD pointer back one commit, but leave your files as they were, ready to be re-committed.
Reset commit history and optionally discard or keep changes
Moves the HEAD
pointer back one commit and unstages the changes, leaving them in your working directory.
git reset --mixed HEAD~1
Moves HEAD
back and discards both the staged and working directory changes.
git reset --hard HEAD~1
These commands combine history manipulation with controlling what happens to your local modifications.
Enhanced Logs
The default git log can be a bit plain. You can get a more detailed and visually appealing log with these commands:
This shows the commit message, which files were changed, and how many lines were added or removed.
git log — decorate — stat
This provides a compact, graphical view of your commit history, making it easier to see branches and merges.
git log - graph - oneline - decorate
https://youtu.be/3gz4DxYix7Y?embedable=true
Conclusion
I hope you found these advanced Git commands helpful. If you liked this tutorial, please give it a thumbs up and share it with other developers.
If you want to share other useful commands, please do it in the comments below!
Cheers! ;)