Important GIT Commands




Below are the few important GIT commands:

Initialize new GIT repository

git init

Initialized empty Git repository in C:/git/.git/


Check GIT repository status

$ git status

On branch master 

No commits yet 

nothing to commit (create/copy files and use "git add" to track)


Add remote origin to GIT repository

$ git remote add origin "https://github.com/gittest/GitTest.git"


List all branches of GIT repository

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git branch -a

* master

  remotes/origin/main


Add remote origin to GIT repository

$ git pull origin master

fatal: couldn't find remote ref master

 

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git pull origin main

remote: Enumerating objects: 3, done.

remote: Counting objects: 100% (3/3), done.

remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0

Unpacking objects: 100% (3/3), 613 bytes | 51.00 KiB/s, done.

From https://github.com/gurpsing/GitTest

 * branch            main       -> FETCH_HEAD

 * [new branch]      main       -> origin/main


Check GIT repository status

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git status

On branch master

nothing to commit, working tree clean

 

After adding a file:

$ git status

On branch master

Untracked files:

  (use "git add <file>..." to include in what will be committed)

        Test.txt

 

nothing added to commit but untracked files present (use "git add" to track)


Adding files to the index

gsingh@ITEM-S1\ MINGW64 /c/git (master)

$ git add Test.txt


Check GIT repository status

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git status

On branch master

Changes to be committed:

  (use "git restore --staged <file>..." to unstage)

        new file:   Test.txt


Commit changes in GIT repository 

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git commit -m "First Commit"

[master 2325d15] First Commit

 1 file changed, 1 insertion(+)

 create mode 100644 Test.txt

 

or

 

$ git commit -a -m "First Commit"

 

 

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git status

On branch master

nothing to commit, working tree clean

 

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git commit -m "Another Commit"

On branch master

nothing to commit, working tree clean

 

After adding three new files and modifying existing file:

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git status

On branch master

Changes not staged for commit:

  (use "git add <file>..." to update what will be committed)

  (use "git restore <file>..." to discard changes in working directory)

        modified:   Test.txt

 

Untracked files:

  (use "git add <file>..." to include in what will be committed)

        File1.txt

        File2.txt

        File3.txt

 

no changes added to commit (use "git add" and/or "git commit -a")



Adding multiple files to staging

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git add File1.txt File2.txt

 

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git status

On branch master

Changes to be committed:

  (use "git restore --staged <file>..." to unstage)

        new file:   File1.txt

        new file:   File2.txt

 

Changes not staged for commit:

  (use "git add <file>..." to update what will be committed)

  (use "git restore <file>..." to discard changes in working directory)

        modified:   Test.txt

 

Untracked files:

  (use "git add <file>..." to include in what will be committed)

        File3.txt


Remove all  files from staging

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git reset

Unstaged changes after reset:

M       Test.txt

 

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git status

On branch master

Changes not staged for commit:

  (use "git add <file>..." to update what will be committed)

  (use "git restore <file>..." to discard changes in working directory)

        modified:   Test.txt

 

Untracked files:

  (use "git add <file>..." to include in what will be committed)

        File1.txt

        File2.txt

        File3.txt

 

no changes added to commit (use "git add" and/or "git commit -a")


Adding all files to staging

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git add -A

 

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git status

On branch master

Changes to be committed:

  (use "git restore --staged <file>..." to unstage)

        new file:   File1.txt

        new file:   File2.txt

        new file:   File3.txt

        modified:   Test.txt


Remove multiple  files from staging

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git reset File2.txt File3.txt

 

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git status

On branch master

Changes to be committed:

  (use "git restore --staged <file>..." to unstage)

        new file:   File1.txt

        modified:   Test.txt

 

Untracked files:

  (use "git add <file>..." to include in what will be committed)

        File2.txt

        File3.txt

 

 

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git commit -m "New Commit"

[master d42ea62] New Commit

 4 files changed, 4 insertions(+), 1 deletion(-)

 create mode 100644 File1.txt

 create mode 100644 File2.txt

 create mode 100644 File3.txt

 

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git status

On branch master

nothing to commit, working tree clean


Checking the log of GIT repository

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git log

commit d42ea62f8758ee2540ce4fd53848d13063b999b0 (HEAD -> master)

Author: Gurpreet Singh <44372365+gurpsing@users.noreply.github.com>

Date:   Wed Jan 4 11:49:57 2023 +0530

 

    New Commit

 

commit 2325d15482a564cfa192053af0821c1c941839e4

Author: Gurpreet Singh <44372365+gurpsing@users.noreply.github.com>

Date:   Tue Jan 3 19:19:06 2023 +0530

 

    First Commit

 

commit 678bb857fcffae552fcb54a35cbbd291039862b7 (origin/main)

Author: Gurpreet Singh <44372365+gurpsing@users.noreply.github.com>

Date:   Tue Jan 3 14:55:17 2023 +0530

 

    Initial commit

 

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git reflog

d42ea62 (HEAD -> master) HEAD@{0}: commit: New Commit

2325d15 HEAD@{1}: reset: moving to HEAD

2325d15 HEAD@{2}: reset: moving to HEAD

2325d15 HEAD@{3}: commit: First Commit

678bb85 (origin/main) HEAD@{4}: initial pull


Reverting the commit

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git revert d42ea62

[master fb9b2ef] Revert "New Commit"

 4 files changed, 1 insertion(+), 4 deletions(-)

 delete mode 100644 File1.txt

 delete mode 100644 File2.txt

 delete mode 100644 File3.txt

 

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git status

On branch master

nothing to commit, working tree clean

 

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git log

commit fb9b2efd498df5c8951f4ba22f398ef6e7e57b2f (HEAD -> master)

Author: Gurpreet Singh <44372365+gurpsing@users.noreply.github.com>

Date:   Wed Jan 4 11:53:00 2023 +0530

 

    Revert "New Commit"

 

    This reverts commit d42ea62f8758ee2540ce4fd53848d13063b999b0.

 

commit d42ea62f8758ee2540ce4fd53848d13063b999b0

Author: Gurpreet Singh <44372365+gurpsing@users.noreply.github.com>

Date:   Wed Jan 4 11:49:57 2023 +0530

 

    New Commit

 

commit 2325d15482a564cfa192053af0821c1c941839e4

Author: Gurpreet Singh <44372365+gurpsing@users.noreply.github.com>

Date:   Tue Jan 3 19:19:06 2023 +0530

 

    First Commit

 

commit 678bb857fcffae552fcb54a35cbbd291039862b7 (origin/main)

Author: Gurpreet Singh <44372365+gurpsing@users.noreply.github.com>

Date:   Tue Jan 3 14:55:17 2023 +0530

 

    Initial commit

 

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git revert fb9b2efd498df5c8951f4ba22f398ef6e7e57b2f

[master a2306b8] Revert "Revert "New Commit""

 4 files changed, 4 insertions(+), 1 deletion(-)

 create mode 100644 File1.txt

 create mode 100644 File2.txt

 create mode 100644 File3.txt

 

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git status

On branch master

nothing to commit, working tree clean



Commit few files from tracked files

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git status

On branch master

Changes not staged for commit:

  (use "git add <file>..." to update what will be committed)

  (use "git restore <file>..." to discard changes in working directory)

        modified:   File1.txt

        modified:   File2.txt

        modified:   File3.txt

 

no changes added to commit (use "git add" and/or "git commit -a")

 

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git add -A

 

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git commit File1.txt File2.txt -m "Commit 04-Jan-2023"

[master a6b6021] Commit 04-Jan-2023

 2 files changed, 2 insertions(+), 2 deletions(-)

 

gsingh@ITEM-S1 MINGW64 /c/git (master)

$ git status

On branch master

Changes to be committed:

  (use "git restore --staged <file>..." to unstage)

        modified:   File3.txt

 


Comments