Some less popular git commands
Tags: git, github, githunt, gitcommands
Published: 2022-10-16 02:38:48

## Introduction

Git is pretty much used by every developer nowadays. Personally, even after using it for approximately two years, I continue to discover new git features. So here are some of the commands I discovered, and seems they aren't popular enough. Without wasting time, let's dive into it.


## Check logs but in easier ways

`git log` a useful tool for looking back and reading the history of every action taken with a repository. A git log's multiple settings can be utilized to narrow down history. The git log typically contains a list of commits. And sometimes the list is so big it's hard to find anything we need. Let me show you an easier way to do the same, but the output is short yet brief.

### git log --oneline
It's super helpful to check the latest commits. With the `--oneline` flag, each commit is reduced to a single line. It just shows the commit ID and the first line of the commit message by default. The output should look like:
```
1266f06 (origin/develop) updated readme
33bf5fb Merge branch 'develop'
bd99bba fixed error
19389a7 Merge pull request #5 from akashrchandran/develop
5a2258e added exceptions
9dbb9de updated readme
```

### git shortlog
A modified version of the git log command called `git shortlog` is used mostly to make release notifications. It displays the first line of each commit message and groups each commit according to author. This makes it simple to determine who has been working on what.

```
Akash R Chandran (4):
      Initial commit
      fixed all errors
      added gitignore
      Added support for heroku deployment

Paulo Martini(3):
      added files via upload
      updated
      fixed requests errors
```
### git log --graph
The branch structure of the commit history is represented via an ASCII graph when the `--graph` option is used. To make it simpler to determine which commit belongs to which branch, this is frequently used in conjunction with the `--oneline`  and `--decorate` commands: 
```
*   64f95a3 (HEAD -> main, origin/main, origin/HEAD) Merge pull request #6 from akashrchandran/develop
|\
| * 1266f06 (origin/develop) updated readme
* | 33bf5fb Merge branch 'develop'
|\|
| * bd99bba fixed error
* | 19389a7 Merge pull request #5 from akashrchandran/develop
|\|
| * 5a2258e added exceptions
|/
* 9dbb9de updated readme
```
To get more info on `git log` you can visit [here](https://www.atlassian.com/git/tutorials/git-log).

## Switching branches
The `git checkout` command can be used for a wide range of purposes. Because of this, the Git community has decided to release a new command: `git switch`. It was designed primarily for the process of switching branches, as the name suggests. 

```bash
$ git switch develop
Switched to a new branch 'develop'
branch 'develop' set up to track 'origin/develop'.
```
### Switch back and forth between two branches
`git switch` makes it very easy to switch between the same branches back to back. It doesn't even need the branch name to switch, just remove branch name and put a hyphen`(-)`:
```bash
$ git switch -
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
```
Using the same command again will switch back to `develop`.

## Checking the changes made to file
The function of diffing outputs the differences between two input data sets. A diff operation is performed on Git data sources when the Git command `git diff` is invoked. Commits, branches, files, and other types of data sources are examples. This document will go over typical `git diff` commands and diffing work flow patterns. `git diff`, `git status`, and `git log` are frequently used to examine the present state of a Git repository. 

```bash
$ git diff 
diff --git a/spotify.php b/spotify.php
index 6987fef..bc766cf 100644
--- a/spotify.php
+++ b/spotify.php
@@ -64,6 +64,7 @@ class Spotify
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                curl_setopt($ch, CURLOPT_URL, $formated_url);
                $result = curl_exec($ch);
+               echo $result;
                return $result;
        }
 }
```

## Restore to an older version of the file
Another extremely useful option available with the git restore command is "—source". You can quickly restore any earlier version of a particular file with the use of this option: 

```bash
git restore --source 6bcf266b index.html
```

## Adding comments to your commits
To preserve a better development history, developers typically write clear and self-explanatory commit notes. Thanks to well-liked commit conventions, some development teams even automatically produce release notes with commit messages. How can I include some extra notes in a commit? Commit message additions will be reflected on remote Git hosting GUIs. How can I record a message for a CI/CD server or add a hidden note to remember about any technical work? We can add extra comments for commits with the help of the built-in Git notes functionality.

To connect a note to the current commit, issue the following command:
```bash
$ git notes add -m "This is a test comment"
```
By omitting the -m option, you can use the editor programme to write a multiline, lengthy note. The git notes show command allows you to view the most recent note. Additionally, the git log command by default displays notes.
Git notes were previously supported by GitHub as comments, but they are now deprecated in favour of web-based comments.

Git does not automatically push or pull notes, but you can manage remote notes explicitly using the following commands:
```bash
git push <remote> refs/notes/*
git fetch origin refs/notes/*:refs/notes/*
```

## Print the SHA1 hashes

`git rev-parse` is an ancillary plumbing command primarily used for manipulation.
One common usage of `git rev-parse` is to print the SHA1 hashes given a revision specifier. In addition, it has various options to format this output, such as --short for printing a shorter unique SHA1.
```bash
$ git rev-parse HEAD
64f95a3edf8ecaf1f874b9a25fb7312a722f463a
```

## Conclusion
These commands are mostly for better productivity and simplicity. It's good if you know what they do. Git is well known these days, but many of its powers are still unknown to the public. It's true that you can "survive" with a handful of commands like commit, push, and pull.

Thanks for reading 😊.