My favorite Git Commands
Check my most used git commands and some tips
I have been using Git for several years now. I use tools like IntelliJ to interact with git, but most of the time I prefer the commands line, which I find easier and faster to work with.
Because of that, I collected a list of the most used commands in my day-to-day work, and I would like to share it with you.
Git Config
The first thing I do whenever I have to configure a new environment is to configure my user name, email, and editor.
git config --global user.name "User Last Namer"
git config --global user.email user@gmail.com
git config --list
Config Editor
Usually what helps me a lot is to have space and be able to add a good description to my commits and because of that I usually define Atom as the git default editor.
git config --global core.editor "atom --wait"
You can choose your favorite editor, check some other examples here.
Git Add
As a simple way to add some changes to a commit, you can use git add file_name
.
But I prefer to check the changes and group them together by using git add -p
which is interactive and very easier.
With git add also has other commands helpful:
- Check the current status of git:
git status
- See the differences:
git diff
- See the differences of add code:
git diff --cached
- Reset add files:
git reset file_name
- Revert changes in single file:
git checkout file_name
Git Commit
Git commit is the most simple and used command in general.
git commit
I can give a little tip, it is possible to commit with the message with the -m
option, but don't use it, take the chance to open your favorite editor and write meaningfully commit messages.1
Git commit amend is also helpful. You just commit something and just realize that one file change was missing from the commit, so you can just amend the change and it will be considered the same block of changes. It can be also used to rename the message from your previous commit.
git commit --amend
I use it only when the pull request is in draft mode, for example, or without anyone else working with me. Just to be safe and don't mess with the git tree.
Git Pull
Pull remote changes to your branch: git pull origin <branch_name>
Git Log
To check the last changes in the current branch, the git log lists the latest commit messages with a complete description.
git log
In case you are looking for too many messages you can make it simple to show in one line the commit SHA and the commit message (which fits in one line):
git log --pretty=oneline
Git Show
To show the last commit's complete details: git show
To show specific commit changes: git show [SHA]
Git Branch
List local branches: git branch
Rename current branch: git branch -m <renamed-branch-name>
Delete Branch
There are several ways to delete a branch, but first, you need to identify the type of branch you want to delete.
Delete local branch git branch -d <branch-to-delete-name>
Delete local branch ignoring errors: git branch -D <branch-to-delete-name>
Delete remote branch: git push origin :<branch-to-delete-name>
Temporary Changes with git stash
This is one of my most used commands.
You know when you create draft changes, they are still not ready to be committed? Just stash it! It is possible to check it anytime and take it back whenever you need:
git stash
To apply the latest stashed code: git stash pop
To list all stashed code: git stash list
Considering the previous list of stashed codes, it is possible to apply any stashed code:
git stash apply stash@{2}
it will take the third last stashed code.
To delete the last stashed codes: git stash drop
Rewrite History
Rewriting branches, updating commits, and clearing history can be tricky but if you know all the options available, you will realize that almost everything can be undone, so why not make use of these commands?
Clear staging area, rewrite the working tree from the master branch:
git reset --hard origin/master
Or It can be from a specified commit message:
git reset --hard [commit]
Git rebase is helpful in situations when you already did commit many times in your branch and before you create your final pull request you would like to make the comments in a different order, rename a commit message, or all of that at once, with the interactive mode -i
option:
Rebase interactively the branch from the remote master: git rebase -i origin/main
You can apply all the commands at once:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous commit's log message
From the previous example, the outcome is 3 reword messages and 1 fixup commit.
Now you have to push the changes to your remote branch, but it is necessary to force push the changes to be applied. The most common commands are -force
, or -f
but I prefer to use +
considering is easier to type one character instead of two.
git push -f origin <branch-name>
or git push -f
.
A safe option to make sure you don't overwrite any teammate code is the paramforce-with-lease
.
git push origin <branch-name> --force-with-lease
or git push --force-with-lease
I had some examples when I was working with rebase and doing a lot of things and at some point, I realise that something went wrong. And I was worried that I had lost my work and had to do everything again, but gladly I was introduced to reflog.
git reflog
Reference logs, or "git reflog", record when the tips of branches and other references were updated in the local repository. Reflogs are useful in various Git commands, to specify the old value of a reference. 2
In other words, I can go back in time and revert any mess I have done in my local repository. This command safe me a lot of time in the past. I hope this helps you as well.
If you want to see the changes in the reflog: git reflog show
and you can check out this change and take it back from the history.
Cherry-pick
Apply the changes introduced by some existing commits.
git cherry-pick <commit-hash>
Alias
If we want to avoid extra typing, why not create some alias? Here is how you can do that:
Create an alias to git status: git config --global alias.st “status”
now you can just use git st
Final Tip
Zsh terminal with Oh-my-Zsh
- It shows colors
- Also has good visibility of your current branch
- Extra autocomplete for commands
- You can use the git plugin which has by default [alias].(github.com/ohmyzsh/ohmyzsh/tree/master/plug..)
For example,
ggpush
which does the same asgit push origin <branch-name>
but with less typing.
These are my most used commands and tips, please share yours in the comments.