Learning Git isn't easy. There are tons of confusing and complicated commands. Because of this, most people only learn the absolute basics of Git such as adding and committing files. This is only a small part of what you can do with Git. In this article, we'll be looking at 10 Git commands that everyone should know, or maybe you're already familiar with that.
1. Add/Commit All
One of the most common things you will do when working with Git is adding and committing a bunch of files at once. The standard way to do this is usually running an add command followed by a commit.
git add .
git commit -m "Message"
This is fine but you can actually combine them together into a one command
git commit -a -m "Message"
#or
git commit -am "Message"
By using the -a
flag when committing you are telling Git to add all files that have been modified and then commit them with the message. This runs into issues with new files. Since the -a
flag only adds modified files it will not add new files or deleted files. Unfortunately, you cannot use commit to add these types of files, but you can use the -A
flag with add to add them all.
git add -A
git commit -m "Message"
This using this you can add all the files modified as well as new and deleted files -A
flag will add all files within the repository.
2. Aliases
In the previous section, we see that how you can add and commit all files, but it requires two commands and if you wanna do it several times then it hurts so much doesn't it? That is where the Git aliases command comes in. With aliases, you can write your own Git commands that do anything you want. Let’s take a look at how you would write an alias for the above add/commit command.
git config --global alias.ac "!git add -A && git commit -m"
With this simple line, we are modifying our global Git config and adding an alias called ac
which will run the command git add -A && git commit -m
. The code looks a little bit confusing, but the result is that I can now run git ac "Message"
and it will do the full add and commit for me.
Now, let's look at what's going on. The first part of the command is git config --global
. This just says we are modifying global Git config
because as we want this alias to be available in any Git repository after that you can use git ac
anywhere.
The next part is alias.ac
. This says we want to create an alias called ac
which is stands for auto-commit. you can name it dinosaur if you want.
Finally, the last part is the full command !git add - A && git commit -m
. This is just our normal Git command, but we have prefixed the command with an exclamation point !
. The reason for this is that a Git alias by default assumes that you will be calling one single git
command, but we want to run two commands. By prefixing our command with an exclamation (!
) git will not assume we are running one simple command. To explain this further here is an example of creating an alias for git commit -a -m "Message"
git config --global alias.ac "commit -a -m"
This is the single git command via using alias. Git will assume that we are trying to call a single Git command and will add the git for us
3. Revert
The last commands have been pretty complex so let’s look at a really simple command. The revert
command simply allows us to undo any commit on the current branch.
git revert 486bdb2
All you need to do is pass the commit you want to revert to the command and it will undo all changes from that commit. One important thing to note, though, is that this only undoes changes from that exact commit.
Another important thing to note is that using revert
does not actually remove the old commit. Instead it creates a new commit that undoes all the changes from the old commit. This is good since it will preserve the history of your repository.
One useful command that revert the most recent commit -
git revert HEAD
4. Reflog
Another simple, but useful command is reflog. This command lets you easily see the recent commits, pulls, resets, pushes, etc on your local machine.
5. Pretty Logs
Another useful logging command in Git is the log command. This command combined with some special flags gives you the ability to print out a pretty log of your commits/branches. As you know the standard git log
doesn't look pretty.
git log --graph --decorate --oneline
For more flags you can visit here
6. Searching Logs
You can also use the log command to search for specific changes in the code. For example you can search for the text A promise in JavaScript is very similar as follows.
git log -S "A promise in JavaScript is very similar"
This command returns the commit where I added the article on JavaScript promises since that is the commit where I added this text.
7. Stash
How many times have you been working on a feature when an urgent bug report comes in and you have to put all your current code on hold. It is very tempting to do a simple add/commit. so you can switch branches to the main branch before fixing the bug. . Instead, the best thing you can do is use a stash
.
git stash
This simple command will stash all your code changes, but does not actually commit them. Instead it stores them locally on your computer inside a stash which can be accessed later.
git stash pop
This command will take all the changes from the stash and apply them to your current branch and also remove the code from the stash.
$ git stash save "<stash-message>"
This will stash your changes with the message you entered. This can be helpful especially when you have several stashes.
git stash list
This might show the following list if you have multiple stashes
stash@{0}: On master: Stashed with message1
stash@{1}: On master: Stashed with message2
Now to use the particular stash you can use the following commands:
git stash apply stash@{1}
Now go and fix your bug without any headache.
8. Remove Dead Branches
If you are working on any decent sized project odds are your repository has tens or hundreds of branches from previous pull requests. Most of these branches have probably been merged already and are deleted from the remote repository, but they are still on your local machine. This can get annoying when you have hundreds of dead branches on your machine which is where this command comes in.
git remote update --prune
This command will delete all the tracking information for branches that are on your local machine that are not in the remote repository
9. Git Merge
Once you’re done with development inside your feature branch and tested your code, you can merge your branch with the parent branch.
We must first switch to the parent branch using the checkout command.
git checkout <parent-branch-name>
Before merging, you must make sure that you update your local parent branch. This is important because your teammates might’ve merged into that branch while you were working on your feature. We do this by running the pull command - git pull
If there are no conflicts while pulling the updates, you can finally merge your feature1
branch into the master
or parent branch.
git merge feature1
This will merge the feature1
branch to master
if your parent branch is master
10. Destroy Local Changes
Sometimes you make changes and realize that you need to scrap everything you have done so far. This usually isn’t a big deal if you haven’t committed yet, but if you have made multiple commits it can be hard to exactly remove all changes. This is where the reset command comes in. By running the below command you can wipe out all changes on your local branch to exactly what is in the remote branch.
git reset --hard origin/main
It will pull the exact code from the remote repository and remove al the changes you have made locally.
Conclusion
There are hundreds of other amazing Git commands I could have covered but these 10 commands are some of my favorites when it comes to really being a power user of Git. Hopefully, at least one of these commands can help you with mastering Git.