Sunday, August 8, 2021

Basic Commands of Git for Beginners

Must read

In this blog, you will get to know all the basic commands for git which are commonly used by all the git users. A simple description of the command is also given with the commands. Still, if you have any questions regarding any particular command you can let me know in the comment section

Global Configuration in Git

The command for setting global Name and Email in your Local machine for git

$ git config --global user.name "[your name]"
$ git config --global user.email [email]

Adding Aliases in Git

Git gives functionality to set your own Aliases for commands. For this, you use the “alias” Keyword followed by a period symbol (.) and then the short command you want to work with.

$ git config --global alias.st status
$ git config --global alias.co checkout

To use the command now, you will time as follows :

$ git st
$ git co

Creating Git Repository on Local Machine

$ mkdir [directory name]

This will create a Folder/Directory in your local machine

$ cd [directory name]

This command will move into the Folder/Directory you have just created

$ git init

This command will initialize the current folder and create a “.git” folder in the directory. This “.git” folder will be hidden by default.

Check Git Status

$ git status

Check the status of files. Whether the files are modified, if modified are they added to a staging area, or are the files untracked which means the new files are created.

Adding File in Git

$ git add [Full file name with path ]  // git add src/Controller/AppController.php

or to add all the files

$ git add [Full file name with path ]  // git add src/Controller/AppController.php

or to add all the files

$ git add ALL

Git Commit Command

Making commit to the changes we just have added. Now when we use add command we ask git to make the files ready. Not when we are done with adding we need to finalize the changes for that we use the commit command

$ git commit -m "[message]"

Git Push Command

Now we have finalized the changes using the commit command, we have to send the data to our remote repository “GitHub” or “Bitbucket”, for we use the commit command Send updates to the remote repository.

$ git push origin [Branch Name]

Creating a new  Branch

$ git checkout -b [New Branch Name]

Delete a Branch In Git


$ git branch -d [Branch Name]

Check all Branches in Git

This shows all the branches you have on your local machine and also the current branch you are working in with a Star Symbol (*).

$ git branch

Changing or Switching the Branch

$ git checkout [branch]

Clone a Repository


$ git clone origin [new folder]

Update local repository

$ git pull

Or if you want to pull the data from the remote repository for any branch say “master” branch then.


$ git pull origin master

Show changed files in Git

$ git diff [Full file name with path ]  // git add src/Controller/AppController.php

Checks changes between two commits

git diff [Commit ID1] [Commit ID2]

Check History of commits

$ git log

Check changes in a specific commit

$ git show [Commit ID]

Check changes in a file commit

$ git show [Commit ID] [Full File Path]

Moving back to the previous commit totally discard the current commit

Note: Be careful with this process/command because there is no way to recover once the data is gone.

$ git reset HEAD ^ --hard

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article