Git bloom
Tags: github, git
Published: 2021-08-12 10:35:38

This post contains the collection of examples of major git cli commands that we normally use in our daily office work. I have grouped them and organised in such a way that it will be easy to remember them and utilise them.
## Pictorial representation
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/npd8kfovuseixz3jdhjd.png)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sr9t25dir4yv2nniu68m.png)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pfo4wb64u0vik65viipr.png)
   
## Startup
- To turn any `directory` into `git repo`
```bat
git init
```
- After that add `remote` of any `github` repo to link your repo to remote
- `or` You can just `clone` a repo if you have any
```bat
git clone <your github repo url>
```
### To view `logs`
```bat
git log
```
### To check `status` of tracked / untracked files
```bat
git status
```
## Remotes
### List down all the remotes information
```bat
git remote -vv
```
### Adding a `new remote`
```bat
git remote add origin git@github.com:mumarkhan999/css-tricks.git
```
### Edit an `already added remote`
- Here `origin` is the name of the `remote` that you wanna edit
```bat
git remote set-url origin git@github.com:mumarkhan999/elephant-fight-in-flex-layout.git
```
### `Remove` already added origin
- You will replace `origin` with your remote name
```bat
git remote remove origin
```
### `Fetch` data from `remote`
```bat
git fetch origin
```
- it will bring branch names, meta data e.t.c.

## Branching
### List down `local branches`
```bat
git branch
```
### List down all branches
```bat
git branch -a
```
### Creating a branch
```bat
git branch my_new_branch
git checkout my_new_branch
```
### Creating and checking out branch in `1 step`
```bat
git checkout -b my_new_branch
```
### Creating a branch from `another branch` not from the current one
```bat
git checkout -b my_new_branch not_current_branch_name
```
### Checkout to the last branch which you were on `without  name`
```bat
git checkout -
```

### Print `current branch` name
```bat
git rev-parse --abbrev-ref HEAD
```
### Pushing a local branch to a remote branch having different name
```bat
git checkout -b my_local_master origin/master
...
git push origin my_local_master:master
```

You can even push different branch to a different branch on remote
```bat
git push origin develop:master
```
This will make the master on remote to the exact replica of develop from local
### `Force` push
```bat
git push origin branch_name -f
```
### `Fetch` and `Pull` of branch
- `fetch` will just bring your branch from remote
```bat
git fetch origin develop
```
- `pull` will `fetch` as well as `merge` the incoming data into `current branch`
```bat
git pull origin develop
```
#### `pull` with default which is `--merge`
- If you pull remote changes with the flag --merge, which is also the default, then your local changes are merged with the remote changes. This results in a merge commit that points to the latest local commit and the latest remote commit.
```bat
git pull --merge
```
#### `pull` with `--rebase`
- If you pull remote changes with the flag --rebase, then your local changes are reapplied on top of the remote changes.
```bat
git pull --rebase
```
### Deleting multiple git branches using regular expression
You can use the git command as mentioned below to delete multiple git branches at once using regular expression
```bat
git branch -D `git branch | grep -i "your regular expression"`
```
### List down all the branches information
```bat
git branch -vv
```
### `Rename` current branch
```bat
git branch -m new-name
```
### `Rename` if you are on `other branch`
```bat
git branch -m old-name new-name
```
### Delete the old-name remote branch and push the new-name local branch
```bat
git push origin :old-name new-name
```
### Reset the upstream branch for the new-name local branch
- Switch to the branch and then
```bat
git push origin -u new-name
```
### `Deleting` a branch
```bat
git branch -D branch_name
```
- `-D` is for forcefully

### `Deleting` unnecessary branches of a `remote`
- This will list down the branches which are available to be `deleted`
```bat
git remote prune origin --dry-run
```
- This will delete them, it's better that you run both of these commands
```bat
git remote prune origin
```
### Bring specific file from `another branch`
- Let's say you are on `master`
- You wanna bring specific file from `develop`
```bat
git checkout develop -- lms/djangoapps/courseware/admin.py
```
## Adding
### Adding `all`
- All the `newly` created files are `untracked` one
- `git` doesn't `track` them
- To make them trackable you `add` them individually
```bat
git add new1.txt new2.txt
```
- `or`
```bat
git add -A
```
- `or`
```bat
git add directory/*
```
### Adding only `tracked files`
- Now let's say there are two types of files in your `working directory`
- tracked / untracked
- You only want to `commit` tracked one so
```bat
git add -u
```
- will do the job

## Committing

### Amend last commit message
```bat
git commit --amend -m "New commit message."
```
### Amend last commit without editing its message
```bat
git commit --amend --no-edit
```
## Tagging
### Moving git tag to the latest commit
Let's say that you have a branch having three commits. On first commit you have a tag, but you wanna move it to the latest one.
```bat
git tag -af <tag_name_on_first_commit>
git push origin --tags -f
```
After this you may need to update the tag/release description on github.

In the above scenerio we moved the tag to the latest commit means the third one, but it is also possible to move it from first to second commit. You just need to specify the commit hash
```bat
git tag -af <tag_name_on_first_commit> <commit_hash_of_second_commit>
git push origin --tags -f
```
### `Fetching` tags from `remote`
```bat
git fetch --tags --all
```
## Rebasing
### Rebasing with `other branch` e.g. `master`
```bat
git rebase master
```
### Rebasing (with only specific range of commits)
```bat
git rebase --onto <base_branch> <start_commit_hash> <end_commit_hash>
```
It will create a commit for you (in deattached state) from which you can create a new branch or whatever you want
you can now rebase your <base_branch> with this commit
### Squashing your commits
```bat
# Here 4 is the number of commits that you wanna squash
git rebase -i HEAD~4
```
A file will be opened most probably in vim
Now, leave the first commit with `p` --> `pick` option
and change the rest of commits option to `s` --> `squash`
Save the file, A new file will open having commit messages
Do what ever you want with the commit messages and save it.
## Diff
### `Ignoring` files while running git diff command
```bat
git diff my_branch_name ':!*.min.js' ':!*.js' ':!*.po' ':!*.underscore'
```
### Taking diff of `two branches`
```bat
git diff feature1 feature2
```
### Taking diff of `current branch with another branch`
```bat
git diff other_branch_name
```
### Taking diff with `stash`
```bat
git diff stash@{0} 
# 0 is the number of stashed changes on the stack
```
### Taking diff with `last commit`
```bat
git diff HEAD~1
```
### Taking diff of a `specific file/folder of two different branches`
- You need to use `relatvie` path of the file/folder you wanna compare
```bat
git diff first_branch second_branch -- myfile.cs
```
## Reset
### Undo the changes of single file from staging and un-staging
```bat
git checkout <your_file_name>
```
### Wanna move your file from staged to working directory
```bat
git reset file_name
```
- or to move all
```bat
git reset
```
### Wanna keep your last committed files (~1) in the working directory but not as committed or staged
```bat
git reset HEAD~1
```
### Totally undo all the changes which are in staged or un-staged
```bat
git reset --hard
```
### Totally `sync` current branch with other branch
- Let's say you are on `local_develop` 
- You have done some changes and multiple / single comit
- To make `local_develop` just like `origin/develop`
```bat
git checkout local_develop
git reset --hard origin/develop
# origin is the name of your remote
```
## Stashing
### Don't want to commit but `wanna save the changes`
```bat
git stash
```
### To take back your `saved changes`
```bat
git stash pop
```
### To list down your `saved changes`
```bat
git stash list
```
## cherry-picking
**Note**: Cherry-pick the commits. Don't cherry-pick the merge.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lw4eobe75vhsta226tyi.png)
 
- In its most basic form, you only need to provide the SHA identifier of the commit you want to integrate into your current HEAD branch
- This way, the specified revision will directly be committed to your currently checked-out branch
```bat
git cherry-pick af02e0b
```
- BUT If you would like to make some further modifications, you can also instruct Git to only add the commit's changes to your Working Copy - without directly committing them
```bat
git cherry-pick af02e0b --no-commit
```