Git Basic Commands

  1. Telling Git Who you are.

Configure the author's name and email address to be used with the commits.

2. Creating a new local repository

Create a folder in your local machine and then run the below command within the directory.

The above command will create a repository in your local system and If you run ls -la in the current directory, You can find a folder .git is created.

3. Adding one or more files to Git

To add a file to the Staging area

To add more files to a staging area

The staging area are the files that are going to be part of the next commit.

4. Committing Changes

Commit is used for saving changes.

After adding files to the staging area using git add, You can commit those changes but not to the remote repository.

To commit any files that you have added with git add and also commit any files you have changed since then.

The above command only works for tracked files (files that are added to git using .git command).

5. Checking status of Git

Lists all the files that you’ve changed that still need to added or committed.

The above command will list all the files/folders that are not yet committed.

6. To list all the local branches in the current repository

The command lists all the local branches in the current repository.

7. To create a new branch

This command will create a new branch.

To check whether the branch is created or not, Run git branch

8. To delete a branch,

9. witching between branches

If are working on different branches and If you want to switch between them.

If you append -b to the above command, It will create a new branch and also switches to it.

10. Copying remote repository to a local working directory

To create a clone or copy of the targeted remote repository to the local working directory.

If the remote git repository which you are cloning to the local working directory is not public, Then it asks for a password.

11. Fetch & Merge remote repository Changes to Local

The command will fetch for any changes in the remote repository and merge it with the local repository.

12. Tracking Changes

git diff command is used to track the difference between the changes made on the file.

The above command is used to find the differences between commits, branches, files, working trees, etc.

For example, The command will pick a file from the local working directory and check for the changes.

It performs 2 checks, One to compare HEAD to the staging area and one to compare staging area to work-tree.

13. Check differences between the staging area and the latest version

Let's say, You have a file which is already committed , Now you have made changes in that file and staged it.

To check the differences between the branches

The command will show the differences between the two branches.

14. Check the version history of the current branch

The command is used to list all the version history of the current branch.

git log command lists the commits made in the branch in reverse order.

To check the version history of a particular file,

15. To Check log messages and textual difference of a commit

git show command is used to view the changes on the specified commit and the metadata of a commit

16. To tag a specified commit

This command is used to give a tag to a specified commit.

17. To list the tags

using the below command, You can list the tags

If you want to list the tags with a specific tag pattern, append -l to the above command.

18. Merging branches

This command is used to merge the specified branch’s history into the current branch.

For example: Let's say your current branch is a new branch and you want to merge it with the master , If you run the below command all the changes in the master branch will be reflected in the new branch.

19. Connecting Local Repository to the Remote repository.

Using the below command, You can connect your local git repository to the remote git repository

20. Pushing Local branch to Remote repository

if you have worked on a branch locally and you want to push that particular branch to the remote git repository, run the below command.

21. Stashing Git files

Using this command You can temporarily store all the modified tracked files.

To restore the recently stashed files.

To delete the stash

22. Removing a file from Git

The below command will delete the file from the staging area (index)

if you want to delete the file from git and as well as the file system, use the below command.

23. Git Reset

If you have staged a file and you want to unstage it? , Use the below command.

To reset all the changes after the specified commits, But the changes are locally preserved.

To move the HEAD to a specified commit. (Too hard reset files to HEAD on git)



DevOps/Cloud | 2x AWS Certified | Terraform | Gitlab

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store