In the fast-paced world of software development, version control is the unsung hero that keeps chaos at bay. Whether you're working on a personal project or collaborating with a team, the ability to track changes, manage code efficiently, and roll back to the previous state is essential. This is where git, a powerful version control system, comes into play.
As a front-end developer, your codebase is your canvas, and git is your trusted brush. With git, you gain the ability to document your project's history, collaborate seamlessly with teammates, and ensure the stability and reliability of your web applications. Besides the essential git commands, like push
, clone
, pull
and commit
, there are a couple more incredibly useful commands. Let's dive in.
log
When to use
Whenever you want to see the commit history! Especially useful when combined with --oneline
. This command only displays the first 7 characters of the SHA key of each commit, improving readability.
How to use it
Type git log --oneline
. That's it. You can combine it with --graph
to visualize the branches. It's not the prettiest and there are other ways to achieve a similar effect, but it's good to know that git has this as an inbuilt command. The effects may look like this:
Read more
https://git-scm.com/docs/git-log
revert
When to use
Say, you made a commit and the commit messed up your codebase. Or you committed something twice and you want to undo it. This is where git revert
comes in handy: you give it the SHA key of the commit you want to undo and it removes the changes by creating a new commit with rolled back changes.
How to use it
You specify the SHA key (or the first 7 characters) of the commit you want to undo:
git revert [insert the SHA key of the commit you want to remove]
Example
for a git log like this:
git log --oneline
cb76ee4 bad commit
01b56c6 good commit
2e407ce first commit
a command git revert cb76ee4
will undo the changes and remove the last commit. It will create a new commit with the changes that are rolled back.
Read more
https://git-scm.com/docs/git-revert
https://stackoverflow.com/questions/19032296/how-to-use-git-revert
reset
When to use
For bigger fuckups, used to revert several commits and reset the current HEAD to the specified state. It requires the SHA key of the last commit you want to keep and removes anything that was committed after it.
How to use it
You specify the SHA key (or the first 7 characters) of the last commit you want to keep:
git reset [insert the SHA key of the last commit you want to keep]
Example
for a git log like this (same as previously):
git log --oneline
cb76ee4 bad commit
01b56c6 good commit
2e407ce first commit
command git reset 2e407ce
will remove the good commit, the bad commit, and keep the code from the first commit only.
Read more
https://git-scm.com/docs/git-reset
squash
When to use
It happens that you were in a hurry, changed some code, pushed it, and then realized there are some more changes to be made with the same commit message. Or another example: you have a git history full of small commits with meaningless messages. To have a clear commit history with comprehensive messages it's best to squash some commits into one, with a single, meaningful message.
How to use it
Run git rebase
in interactive mode. Select which commits you want to squash. Then select a commit message and push the changes.
Example
Let's say you have 4 commits you want to squash into one. For a git log like this:
git log --oneline
80801e4 more changes
e3873ff update styles
92c2f17 update css
186d599 changes
2e407ce first commit
run git rebase in interactive mode
git rebase -i HEAD~4
this opens the editor window. Change "pick" to "squash" for every commit that you want to be squashed into one, except for the first one. Save changes and close the editor.
choose a commit message, save changes and close the editor.
push changes with
git push --force origin HEAD
Read more
https://git-scm.com/docs/git-rebase
https://prasad-k-pawar.medium.com/how-to-combine-multiple-git-commits-into-one-c04c67367a36
Summary
In this article, we explored four incredibly useful commands: log
, revert
, reset
, and rebase
. These commands are indispensable tools in a developer's git toolkit, providing the ability to visualize commit histories, undo changes, reset branches, and streamline branch management.
git log
offers valuable insights into your project's commit history, helping you understand the evolution of your codebase and facilitating collaboration.git revert
empowers you to gracefully undo specific commits, creating a new commit to reverse changes without altering history.git reset
allows you to reset your branch to a specific commit, whether to unstage changes or start anew.git rebase
provides an advanced way to rewrite and reorganize your commit history, promoting a linear and clean history that's easier to maintain and understand.
Mastering these commands not only enhances your efficiency as a git user but also empowers you to navigate the complexities of version control with confidence. By learning how to properly use git to its full potential, you'll be better equipped to manage your repositories and collaborate effectively with your team. By no means it's an exhaustive list as git offeres so much more. But we'll talk about it some other time. Happy coding!