All The Git Command I Learned So Far

All The Git Command I Learned So Far

I learned a bunch of Git commands recently. To ensure I don't forget them and to help others, I'm documenting it here. All the code tested on LMDE.

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 reference when will send a git push request on a 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 the SSH key to verify that it is you.

2. Verifying SSH Key

Generate an SSH key with the following command:

ssh-keygen -t ed25519 -C "your_email@example.com"

After running this command on the terminal you will get a 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 on all of them and move on. After that you will get a key which will look like this

image of key

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

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 commands 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 literally 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 don't have time for git add command, then place -a before -m, and it will auto-add everything. it is like using git add -A

git commit -m 'your message'

git commit -a -m 'your message'

git commit --amend --no-edit  #it will keep the previous commit id and message same. Used for fixing typo which does not need its own commit.

Viewing Commits

To know if your commit was succesful or not, or to know about previous commits and who and when did it with the 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 a more extensive version of the 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 the difference between two particular commits or branches then better to use

git diff <commit1> <commit2>

git diff <branch_name> <branch_name>

Tt will show the changes between the two commit.

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

Branching

To change the branch 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 are multiple options git provides to rollback to the previous commit and revert is the safest option. It rollback to any previous 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 running 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 resolve the conflict manually. If the conflict 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.

Git Clone

To clone a repository and work on it use the following commands.

git clone <ssh of the repository> #it will clone the main branch
git clone -b <branch name> <ssh of the repository> #to clone specific branch

4. Less used command

Remove and Move

Many of you might not need this command but it never hurts to learn. The first command is the mv command. Linux users are 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 the 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 the 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 the previous commit and modifies it under the same previous commit id. It is helpful when you made some typos or silly mistakes and you don't 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 understand 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 is 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 button on the github website.

  4. Press it and and pull the request based on their guideline.

  5. if you had to modify the pull request and push it again, then there 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 a merge error.

Photo Credit - freeCodeCamp