Understand Git in an easy way....!

Understand Git in an easy way....!

·

7 min read

Hey there! I'm Siva, I am working as a Manual Tester and learning DevOps and writes about DevOps tools and this is my 1st blog🙂. I believe in learning in public. I share my everyday learnings on Twitter to everyone . I am writing this article about Git which is used in almost every project and is very much important for everyone.

Now let us practically learn local git commands. If you've learned git and you are still in confusion with how it works locally then this is for you.

In this article you will understand:

  • Git basic commands.

  • How to add and commit your code changes to your local repository.

  • How to move files from workspace to staging area and vice versa.

  • How to move files from the staging area to the local repository and vice versa.

  • How to move files from the workspace to the local repository directly by skipping the staging area and vice versa.

How does Git work locally?

This image describes the entire article, so use this image as a reference. Let's start!

Install git on your local machine and follow the below commands in the article to learn.

How to Navigate to Git Bash on your local computer.

After installing Git, create a new project folder on your desktop. Open the new project and click on Git Bash here(refer to the below image)

Initialize

  • Initialize command is used to initialize the folder/project repository.

  • Type the git init command to initialize the repository(Please refer to the below screenshot.)

Use the below commands one by one to create two new files in your folder.

touch f1 
touch f2

Now you have created two new files in your folder, let's move on to real git.


Unstaging/Workspace area to Staging area:

Git Status:

  • The git status command lists all the existing modified files and new files you have created.

    Hint: Red-colored file names indicate that they are in the unstaging area and untracked.

  • Files are untracked in the workspace area.

  • Before committing your files to your repository, your files must be tracked.

  • Let's learn how git tracks the files.

Git Add

  • There are two methods to move your files from the unstaging area/workspace(untracked files area) to the staging area(tracking area).
git add f1
git add .
  • Use the first command if you want to move a specific file to the staging area by typing the file name.

  • The second command moves all the unstaged files in your workspace area to the staging area.

  • Now to check the files present in the staged area use the same command

    git status which we used for checking the unstaged files. (Refer to the main image attached starting of this article for understanding the commands structure)

    Tip: Green-colored file names indicate that they are in the staging area and files are being tracked.

Now we have successfully moved our files to the staging area.


Staging area to Local Repository:

Commit

git commit -m “message” f1
git commit -m “message”
  • The message in double quotes describes your commit, it is called a commit message, you can give whatever commit message you want in double-quotes.

The above commands can be used to commit the staging area files to the local repository.

  • The first command is used when you want to commit only a specific file by mentioning the file name after the commit message.

  • The second command moves all your staging area files to the local repository.

After committing the required files, use the git log command to see the list of all the commits you made, the command can be referred to as commit history.

  • The long alpha-numeric key is called as commit id, for every commit you made the commit id will generate automatically.

  • Now create new files and do the above processes and commit them to the local repository, then you can see 2 commits and their IDs in the log.

  • To push the changes to remote(GitHub) you must commit the changes.

  • Whatever the files you committed everything will be present in the commit id.

  • git show commit id shows all file changes.

  • Here I changed the first line of the f1 file from 'Hi there ' to 'changing the first line'.

  • Since I changed the first line, red colored text indicated that it was removed and the green-colored text indicates it was added.

      Shortcuts in Git:
      ctrl + insert = copy
      shift + insert = paste
    

Hint: Since Git is based on Linux, copy-paste commands are different in Git. Use the above commands to copy the commit id.

Now we successfully moved all the files to the local repository.

From here you will learn how to roll back your files from the local repository to the staging area and staging area to the unstaging/workspace area.


Local Repository to Staging Area:

git reset —soft HEAD~n

n=number of commits, if n=3 then the last 3 commits will get reset

This command can be used:

  • When you want to undo the commit/commits.

  • When you forgot to include a file in the commit and you don't want to commit that file separately then you can use this command and then commit all files together.

  • When you don't want the last commit changes in the local repo.

  • I have 2 commits in my log(refer to the above image).

  • I hit the reset command with n=1 and checked the latest log.

  • Now you can see only one commit, because of the reset command the last commit was removed and files related to that commit are back in the staging area.

  • Now check the staged area with git status command.

  • You can observe that the files belonging to the last commit moved from the local repository to the staging area.


Staging Area to Workspace/Unstaging area:

git reset -- filename
git reset
  • Using git reset — filename we can move back files from the staging area to the workspace/unstaging area(untracked files area). Refer to the below screenshot

  • If you type the git reset command without a file name then all files present in the staging area move to the unstaging area.


Local repository to Workspace/unstaging area:

When you mistakenly committed file changes and you want to undo that commit and you want the files to move directly from the local repo to the workspace by skipping the staging area then this command is for you.

git reset HEAD~n
  • Again n refers to the number of commits if n=2 then the last 2 commits’ file changes will be restored to the workspace.

  • Resetting more commits is not recommended but be careful and back up your files when you are resetting more commits.


Finally, you can also directly commit files directly from the workspace to the local repository by skipping the staging area but only on some conditions. Let's go through it.

Workspace to Local repository:

git commit -a -m “message” filename 
git commit -a -m “message”

git commit -a -m “message” filename - committing a single file

git commit -a -m “message” - committing all modified workspace files.

Rules for this command are:

  • This command won’t work for the new files.

  • The files you are going to commit from the workspace must be already committed and should be modified.

After modifying the committed file they will appear in the workspace area, then you can commit the file directly.

Observe the above screenshot:

  • I have 3 files in the workspace area in which f1 and f2 are modified and already committed and f3 is a newly created file.

  • Now after using the git commit -a -m "message" command, you can observe that both files are committed and the new f3 file remained in the workspace area.

  • You can check the same in the log using the git status & git log commands.

Note: Unstaging area/workspace both are referred to as the same area and this is the area where you will be first when you created a new file/folder.

So this is the article/blog. I hope you understood everything and were helpful. If you like it please follow, like, and share. I will keep writing informative blogs related to DevOps. Please give your feedback on the article in the comments. Thank You! Have a nice Day!!!🙂🙂

So that's it about this article!. Thanks for reading till the end.

Â