Git Handbook
Tags: git, github, beginners
Published: 2023-01-23 15:22:30

Hello everyone!

This blog post is going to contain all the git commands with its use cases for quick lookup.

Without further ado, let's jump into it.

## BASICS

0. _**`git init`**_ - to initialise the repository for git to track it.
1. _**`git add <filename>`**_ - adds the untracked file <filename> to stage.
2. **_`git add <file-1> <file-2> <file-3>`_** - adds multiple files based on the specified file names to stage.
3. **_`git add .`_** - adds all the untracked files to stage.
4. **_`git restore -stage <filename>`_** - unstage the <filename> i.e it moves to untracked files list.
5. **_`git stash`_** - moves both the (un-committed) staged and unstaged files to stash (like backstage) for cleaner working directory. 
6. **_`git stash pop`_** - moves back all the files in stash area.
7. **_`git stash clear`_** - clears the files in stash i.e permanently deletes.
8. **_`git commit -m <title> -m <description>`_** - commits the staged/tracked files with the corresponding commit message and description.
9. **_`git log`_** - public record of commit history for a branch.
10. **_`git reflog`_** - private record, saved locally used for recovering deleted branches.
11. **_`git reset <commit-id>`_** - resets the head to the commit id mentioned and the removed commits are pushed to unstaged area.
12. **_`git reset --hard <commit-id>`_** - resets the head to the commit id mentioned and deletes the changes of above commits permenantly.
13. `git revert <commit-id>` - While git reset does this by moving the current head of the branch back to the specified commit, thereby changing the commit history, git revert does this by creating a new commit that undoes the changes in the specified commit and so does not change the history.
14.**_`git push origin <remote-branch>`_** - pushes your commits to remote repository, <remote-branch> thats mentioned in the command.
15. **_`git push origin <remote-branch> -f`_** - force push required when remote branch has some commits which aren't present/removed in local(eg: with git reset).
16. When working with forked projects, we need to fetch upstream commits - **_`git pull upstream develop`_** This creates an extra commit.
17. `git rebase -i <commit-id>`- Squashing your commits above a particular commit id(you can either pick or squash)
18. `git commit --amend -no-edit` - adds the staged files to previous commits without generating a new commit.
19. `git commit --amend` - adds the staged files to previous commit whilst providing an option to edit the previous commit. This and the above are very helpful when some logical step is missed in the previous commit.

## Prefer rebase over merge to pull remote changes to avoid merge commits -

1. `git remote -v` - displays all the remote urls attached to the local repository.
2. `git remote add <remote-name> <remote-url>` - connecting your local git repository with the remote repository hosted on Github with the remote name.  
    - `<remote-name>` is generally origin for the repo cloned from github. It basically depicts the repository that we want to connect to.
    - A repository might have multiple forks in which one of them can be cloned to the local repository. 

3. In order to pull the changes from upstream repo into the fork - `git pull --rebase <remote-name> <feature-branch>`
    - this pulls and rebases the remote branch of upstream repo without making an extra commit on feature/develop branch of forked repo. Feature branches don't need to track merge commits.
    - resolve the merge conflicts in the process of rebase if exists.
    - use `git rebase --continue` once merge conflicts are resolved for a particular commit to continue further.
    - use `git rebase --abort` for aborting the rebasing.

4. If you need to squash any duplicate commits, use `git rebase -i HEAD~N` or `git rebase -i <commit-id>`.

5. DO NOT FORGOT to FORCE PUSH the commits that are squashed or amended. `git push origin develop -f`


## QUESTION

And there's some famous question/doubt everyone has on their mind when starting off with git/github:

**Why is a git 'pull request' not called a 'push request'?**

"Push" is you forcing the changes being present in the target repository (git push).
"Pull" is the target repository grabbing your changes to be present there (git pull from the other repo).

- A "pull request" is you requesting the target repository to please grab your changes.
- A "push request" would be the target repository requesting you to push your changes.

## Always write better commit messages
- Commit history is the index.
- Writing meaningful, logical and verbose commit messages allows others to understand the task or story that the PR solves.
- Prepend the commit message with its the type like feat, chore, fix, test, docs, refactor etc. https://www.freecodecamp.org/news/how-to-write-better-git-commit-messages/
- squash all duplicate commits so that the master branch contains clean commit history.


Thank you!