DEV Community

Cover image for Git commands that you might not have used before.
Ellaine Tolentino
Ellaine Tolentino

Posted on

Git commands that you might not have used before.

Hello! This blog is for my fellow codenewbies out there that are trying to know more about Git/ Git workflow!

I've recently encountered these commands and I thought might as well share it to everyone who is new in using git and are trying to learn more how to maneuver in it. There are so many git commands that needs to be learned, but these are the ones I recently have been able to use!

git log

Lists out your commits starting from the most recent one. It includes the commit reference & the commit message you've attached with it. This is a good tool to utilize when you need to track down your commits. Looks like this;
git log list(To see further, press Enter/Return key, to exit git log, press q)


git log --all --decorate --oneline --graph

If you're like me you like visuals. I didn't know that there is a way to show a graph of your branches, merges & commits in the command line! Once you ran the command, it should show something like this;
CLI command git log
(To see further, press Enter/Return key, to exit git log, press q)


git shortlog

Sorts out each commit by author. Good way to see who's working on what. Base on mine, since I am working alone on this repo, it showed my Github username for the commits, and Github public name for the merges.

CLI shortlog


git cherry-pick

Yes! As the name suggests, you get to cherry-pick through your commits and move one commit from a branch to another! You might think of using git merge that is another way to it if you wanted to keep the history of change in Git. Good example of cherry-picking is for example you have 2 branches called devBranch & branch2a and you had your commit in devBrancg but supposedly need to be in branch2a.
To use it,

  • take a note of the commit reference(Ex.227aaf1) using git log,
  • then checkout to branch2a. Once there,
  • run git cherry-pick [227aaf1] and it will show you the conflicts; cherry-pick conflicts
  • Once resolved, run add and commit the changes.

Visual examples for cherry-picking in Git can be found in GitKraken.


git commit --amend

Seeing your git log, what if you thought of updating the most recent commit, whether to add more detail on the commit message or alter a little of the code?
Example usage of git commit --amend, let say you got your changes saved and you do your regular git add . & commit but your commit message was short like this(according to git log);

amend-See our most recent commit has a very short message and if we want to change that, we run git commit --amend.

In git amendThis will come up, then you have the chance to alter the first line and as you see I changed the git message into "whitespace removed from FlipCard.js" instead of just "space".
To save the changes you did, press Ctrl + C then type :wq and press Enter

After amend-You can confirm it through git log again if it went through matching the new commit reference as well.

Other Vim commands are here and further details or examples about git commit --amend from Git tutorials


git push --force-with-lease

This command is ran after commands like git rebase or git commit --amend. This is a safer option on pushing commits and offers a bit of protection. Here is one source on further explanation for --force-with-lease.


git checkout -

Brings you back to previous branch you were in. (My new current go-to command everytime I forget my branch names.) This command helped me alot while going through the branches for this blog!


There are definitely tons of commands I want to learn and use, like git rebase, git reflog, even be more familiar with git remote. I hope this blog helped you in a way if you are new to git.

If you have any resource visual learning of Git commands, or if I have missed anything, please let me know in the comments below! Thank you! Until the next!

References:
Atlassian
Header icons
GitKraken

Top comments (0)