👉 Play with Git & Its Command

·

6 min read

👉 Play with Git & Its Command

Introduction To Git

Git serves as a version control system primarily responsible for tracking changes within a folder. In essence, it empowers users to maintain control over the evolution of their files and projects. If you're new to Git, it's crucial to grasp the basics, so let's delve into this introductory overview.

Introduction To Github

Just as we utilize cloud-based platforms like Google Drive to store photos and videos for future accessibility, GitHub serves a similar purpose in terms of file and project management. GitHub, or alternatives like GitLab and Bitbucket, acts as a collaborative platform that enables users to store files, folders, and more in a version-controlled environment. Let's explore the significance of this platform further.

Why Prefer Command Line Over Git Desktop GUI Apps on GitHub?

Using the command-line interface in GitHub has distinct advantages, offering clarity and a hacker-like experience. GUI applications often demand memorization of visual icons, leading to potential inefficiency and reduced intuitiveness. To get started with command-line project management, follow the provided steps. Windows users are encouraged to install Git Bash, while Mac and Linux users can execute commands directly in the terminal.

Important Basic command

  1. $ git init

This command grants Git permission to track the folder where future changes or updates will occur. If you're new to Git, begin by creating a folder on your desktop. Then, open the same folder path in Git Bash or your terminal and enter the "git init" command.

Avoid skipping "git init" and directly checking the "git status" of the folder/repository, as it will trigger a fatal error message.

  1. $ git clone url_of_repo

This command facilitates the duplication of a repository locally from the server where it is currently present. Simply input "git clone [URL of the repository]" into your terminal and press enter.

Upon execution, you become the local owner of the project files within your designated folder.

For example: git clone https://github.com/durgeshmehar/Best-C-language.git

  1. $ git status

This command is for checking the current status of the project. It gives information about modifications that happen in which file, branches.

If we do modification in the project file of the local repo and then enter git status, it gives a message of modified: as in red colour.

  1. $ git diff

This command line gives information about which changes are modified by the owner. It shows - for replacing this line with + for the new line added.

  1. $ git add modified_file_name

It is used to move files from unstaged to staged areas for commit. It simple words, it added the changes to the staged area. Staged area is the area where you next commit changes. Now check the git status, our modified: message in green colour which shows we successfully added to the stage.

$ git add . : To add all files in the staging area which has working changes in it.

  1. $ git commit -m "Your_msg_here"

It is used to lock or save the changes of the project with some comments. It generated a commit unique ID, which you can return in future on this change file if we make other changes.

$ git commit -am "your_msg": It is used to directly make unstagged area changes to the commit area. Simply, It is a combination of git add & git commit.

$ git commit --amend -m "Your_msg_here": It is mostly used to change recent commit messages. If you modified file and don't want to make separate comments. Then you think to update recent commits with modified files using the amend command.

  1. $ git log

It shows the total commit (which means lock the changes) on the project file. It shows the commit ID, the branch that we committed, also author's email, and the time and date with a committed message.

$ git log -5: If you want to see your last 5 comments.

  1. $ git show commit_id

It shows the combined view of the git log and git diff command present at commit id.

  1. $ git branch

It is used to know the all of branches.

$ git branch branch_name: Creates a new branch.

$ git checkout branch_name: Move to the entered branch from the current branch.

$ git checkout -b branch_name:It creates & move to new branch.

$ git branch -d branch_name : To delete the branch this $ git branch -d is used. It is unable to delete a branch if you are present at the branch and want to delete this same branch so you must checkout to another branch and then apply the operation of deletion.

$ git merge branch_name : It is used to combine changes from one branch into another branch. while merging you must be in the (master) main branch then you run the command of git merge with the sub-branch.

  1. $ git reset --hard Head^

To delete the latest commit done by the user in the current branch. After removing it automatically goes on the previous commit of the deleted commit and also applies changes.

  1. $ git reset --hard commit_id: It deletes all commits till the entered commit id is along with discarding all modification done at the local working directory.

  2. $ git reset --mixed commit_id: It deletes all commits till the entered commit id is along with shows all changes as modifications at the local working directory.

  3. $ git reset --soft commit_id: It deletes all commits till the entered commit id is along with shows all staged changes at the local working directory. Its safe to use than the above two commands.

  1. $ git restore --staged file_name

It is used to get back into untrack file region from the track region.

It is the reverse process of the git-add command.

$ git restore file_name: It is used to delete all changes done in the folder when the user is in the untract file condition. If the file is in an untract position then you delete these changes and come back to the original structure.

  1. $ git pull

It is used to sync changes that happen in the server project folder with our local folder.

  1. $ git push

It is used to merge requests on the server project folder with our updated folder.

Here Repository maintainer accepted the request and merged it into his project.

  1. $ git tag

    It is a special way to commit that the currently committed file is now ready for testing.

  2. $ git stash

    It is used to temporarily save changes that you've made to your working directory but don't want to commit yet. It's helpful to resolve conflict when any person makes the same changes in the repository at the same file that you are doing & wants to commit it.

$ git stash: list all stash files saved in temporary memory.

Now, First, you have to use git pull to get changes done in the file on the server.

$ git stash apply: To merge both changes done in same file code.

At this stage resolve conflict error. It means, deciding which changes you have to put in the file & after make sure to use git add . follow by git commit -m command.

So, did my Job. You now have a better understanding of commands. I know this is time, effort-taking process but now you are the best who knows all used commands with easy understanding.

Thank you. I look forward to sharing my knowledge.

Â