Important GIT Commands

Here is a list of some important Git commands with examples:

1) git init
This command is used to initialize a new Git repository in the current directory. For example, you can use the following command to initialize a new Git repository in the my-project directory

$ git init my-project

2) git clone
This command is used to clone an existing Git repository from a remote source. For example, you can use the following command to clone the my-project repository from the https://github.com/user/my-project.git URL

$ git clone https://github.com/user/my-project.git

3) git add This command is used to add files to the staging area in a Git repository. For example, you can use the following command to add the file1.txt and file2.txt files to the staging area

$ git add file1.txt file2.txt

4) git commit
This command is used to commit the changes in the staging area to the Git
repository. For example, you can use the following command to commit the changes in the staging area with the commit message “Added new files”

$ git commit -m “Added new files”

5) git push
This command is used to push the committed changes in the local repository to the remote repository. For example, you can use the following command to push the committed changes to the origin remote branch

$ git push origin

6) git pull
This command is used to pull the changes from the remote repository to the local repository. For example, you can use the following command to pull the changes from the origin remote branch

$ git pull origin

7) git branch
This command is used to list, create, or delete branches in a Git repository. For
example, you can use the following command to create a new branch called my-branch

$ git branch my-branch

8) git checkout
This command is used to switch between branches in a Git repository. For
example, you can use the following command to switch to the my-branch branch

$ git checkout my-branch

9) git log
This command is used to display the commit history of a Git repository. For example, you can use the following command to view the commit history with the author and date information

$ git log –pretty=format:”%h %an %ad”

10) git diff
This command is used to view the differences between two versions of a file in a Git repository. For example, you can use the following command to view the differences between the file1.txt file in the HEAD and the my-branch branch

$ git diff HEAD my-branch file1.txt

Leave a Comment

Your email address will not be published. Required fields are marked *

Need Help?