All The Git Command I Learned So Far
Published: 2024-01-08 17:09:07


Hello fellow developers! Recently, I completed a course offered by Google, where I learned a bunch of Git commands. To ensure I don't forget them and to help others, I'm documenting them here. These commands are specifically tested on Linux, particularly on the LMDE (Linux Mint Debian Edition) distribution.

## 1. Initial Setup

First things first, let's set up your Git configuration with your email and username. This username and email will be used as a refference when will send a git push request on remote or local repository.

```
git config --global user.email "me@example.com"
git config --global user.name "My name"
```

After configuring your identity, initialize a Git repository in your project directory.

```
git iniit
```
This creates a hidden folder named .git in your project directory, containing essential Git files. You may also need to verify with SSH key to verify that it is really you.

## 2. Veryfying SSH Key

Generate an SSH key with the following command:

```
ssh-keygen -t ed25519 -C "your_email@example.com"
```
After running this command on terminal you will get bunch of question one after another like

```
> Enter a file in which to save the key (/home/YOU/.ssh/id_ALGORITHM):[Press enter]

> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]
```

just press Enter to all of them and move on. After that you will get a key which will look like this 


[image of key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xog8j4oulchvfjys93yz.png)


After generating the key, add it to the SSH agent and save the identity locally.

```
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
```

After that you will need to read a .pub file to get the public key. The public key will look like this 

[Public key Example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ricka5tx9xputaki039.png)

Use this command to do it.

```
cat ~/.ssh/id_ed25519.pub
```

You can also open it on a text editor. For that you can use command like this

```
nano ~/.ssh/id_ed25519.pub #for nano text editor
gedit ~/.ssh/id_ed25519.pub #for gedit text editor of linux
```
Now copy the whole thing. YES! **from ssh to .com **litteraly everything. After that go to github setting. There you will find SSH and GPG key section, Click it, you will notice a section named SSH keys. From there press new SSH key option and paste the copied value. Now you are good to go.


## 3. Common Git Commands


#### Checking Status
To see changes staged, unstaged, and untracked files:

```
git status
```
#### Staging Changes
If you want to add any new or modified files to the git track list then you have to use this command. you have to use this before using `git commit`. After using this command you can check that your new or modified file added to the tracking system by using `git status`.

```
git add <filename> #this is to add specific file

git add . #this is to add all the newly created or modified under same directory and its child dirctory. this will not remove any delteted file from the previous snapshot

git add -A #you have to use this one for most of the time. it tracks everything

git reset #to undo the staging of your files.
```
#### Committing Changes
After staging the file with `git add` command, now you have to create a saving point. You should write a small but informative message, so that future you or the team can understand why you made this change. 
`-m` after that you have to type your message.
`-a` if you dont have time for git add command, then place -a before -m, it will auto add everything. it is like using git add -A

```
git commit -m 'your message'

git commit -a -m 'your message'
```

#### Viewing Commits
To know if your commit was succesful or not, or to know about previous commits and who and when did it with commit message then use this command.

```
git log

git log --oneline #to get only commit message

git log --graph # Visualize merge structure if the merge is three way or fast forward

git log -p  # Generate patch file with commit differences
```


If you want more extensive version of log whih contains more details then use 

```
git show  #to show all the commits

git show <commit_id/blob/tree>  #to know everything about a particular commit/blob/tree
```
#### Viewing Changes
If you want to get difference between two particular commit or branch then better to use

```
git diff <commit1> <commit2>

git diff <branch_name> <branch_name>
```
Tt will show the changes between two commit.

**In many cases you can use `-n` after the command just to dry-run it. Which means it will do nothing, but it will demostrate what would happen if you actually execute it.**

#### Branching
To change the barnch or change the HEAD pointers toward a specific commit, you need to use this following command

```
git checkout <branch_name>

git checkout -b <new_branch_name>  #it will create a new branch and move the HEAD pointers towards it.

```

To know which branch I am in and how many branch are there use this following command. the *sign indicates which branch you are currently in

```
git branch

git branch <new_branch_name>  #it will create a new branch

git branch -D <name>  #forces the branch to delete. you can also use small -d which sometimes might not delete the branch
```
#### Rollback
There is multiple option git provides to rollback to previous commit and **revert** is the most safest option. It rollsback to any privious commit of your choice. It does create a new commit with previous changes.

```
git revert <commit_hash>

git revert -m "Your Message" <commit_hash>  #with message
```

#### Merging
To merge one branch with the other brach you can use use this command. Before running this command you have to checkout to the branch where you want to merge the branch.

```
git merge <branch_name_that_you_want_to_merge>

git merge --squash  #merge the branches without creating a new commit
```
After runing this command you may find a thing known as merge conflict. Merge confilict means both branch trying to modify the same line of code. After facing this issue you have to ressolve the conflict manually. If the confilct is too major then you can avoid the marge by this command.

```
git merge --abort
```
#### Keeping Updated
To stay updated with your remote repository you can use multiple commands. 

```
git remote update #only fetch informations about the remote repository

git fetch #downloads everything from remote repository but it does not merge automatically

git pull #download update from online and merge with the local repository
```
`git remote update` is the safest bet if you are working on a big project with multiple people. Cause if you pull then it will merge the update with your local repositoy which may create merge conflict and unwanted changes.

## 4. Less used command

#### Remove and Move
Many of you might not need this command but it never hurts to learn. First command is mv command. linux users already familiar with this command. it helps to rename or move a file.

```
git mv <file name> <destination name>
```

To remove the file from the directory, you have to use 

```
git rm <file name>
```

To use `git rm or mv`, first you have to add those file in git tracker by using git add command. Then you will be able to use this commands. This commands will remove the item from git tracker or git file direcotry after commit. But it will not remove physical file from the pc.

#### Amend
To edit a commit you can use this, this truly edits the commit. it combines current staged file with previous commit and modify it under the same previous commit id. It is helpful when you made some typos or silly mistke and you dont want to create a new commit for it.

```
git commit --amend
git commit --amend -m "Message" #it modifys the previous commit message 
```
#### repo name
To know which repository you are working on along with url use this command

```
git remote -v
```

#### Rebase
I did not undestand what this command properly do. But what i did undestand is if you call this command then it will change the current branch to the targeted branch. It will also rewrite the history of that branch. This tool mainly used in pull request.
```
git rebase <target_branch>
```


## 5. How to contribute to a project

1. first you have to fork the project
2. Then open a terminal on a local directory and call `git clone <https link of that file>`
3. Then make changes and push. After that you will get a pull request on button on the github website.
4. Press it and and pull the request based on their guideline.
5. iI you had to modify the pull request and push it again, then their will be multiple commit id, which is not good looking. to fix this you can use `git rebase -i master` and choose **pick** for all the git commit you want to stay there and **squash** if you want to remove them.
6. Then use git show and git status to check if the change applied or not.
7. **git push -f** to force push the change on the repository. so that it does not show merge error.

Photo Credit - freeCodeCamp