Git commands that you might not have used before.
Tags: github, codenewbie, git
Published: 2021-07-29 01:04:50

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hztvhmh5mt08kli89tig.png)(_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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u9no602nvf89px1w7g7q.png)
(_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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/92edgjdx5atqtfa0hvbe.png)
***

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e1r7j8mbme5240s16zya.png)
- Once resolved, run add and commit the changes.

Visual examples for cherry-picking in Git can be found in [GitKraken](https://www.gitkraken.com/learn/git/cherry-pick).
***

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ftfioq1p305k78jkn0rm.png)-_See our most recent commit has a very short message and if we want to change that, we run `git commit --amend`._

![In git amend](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2lnde0wwwyzvsf5j20lj.png)_This 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aa37fj7zjpm2x4z55rht.png)-_You can confirm it through git log again if it went through matching the new commit reference as well._

Other Vim commands are [here](http://www.fprintf.net/vimCheatSheet.html) and further details or examples about git commit --amend from [Git tutorials](https://www.atlassian.com/git/tutorials/rewriting-history)
***

## 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](https://blog.developer.atlassian.com/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](https://www.atlassian.com/git/tutorials/learn-git-with-bitbucket-cloud)
[Header icons](https://www.iconfinder.com/)
[GitKraken](https://www.gitkraken.com/learn/git)