Home DevOPs Git Commands

Git Commands

by Editorial Staff

How to add only certain files to the staging area in Git

With the asterisk in the command below, you can add all files starting with ‘file’ in the staging area.

git add file*

root@vmlinux:/var/lib/git/.git/devops1# git add work* root@vmlinux:/var/lib/git/.git/devops1# git status
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

                           new file: work1 
                           new file: work2 
                           new file: work3

How to check a repository’s status in Git:

This command will show the status of the current repository including staged, unstaged, and untracked files

git status

root@vmlinux:/var/lib/git/.git/devops1# git status On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

                              new file:	5MB.zip
                              new file:	filename1.txt
                              new file: text1.txt
                              new file: text2.txt 
                              new file: text3.txt

How to commit changes in the editor in Git:

This command will open a text editor in the terminal where you can write a full commit message.

A commit message is made up of a short summary of changes, an empty line, and a full description of the changes after it.

git commit

How to commit changes with a message in Git:

You can add a commit message without opening the editor. This command lets you only specify a short summary for your commit message.

root@vmlinux:/var/lib/git/.git/devops1# git commit -m "committing some text files"
[main 6f76b3f] commiting some text files
5 files changed, 0 insertions(+), 0 deletions(-) 
create mode 100644 5MB.zip
create mode 100644 filename1.txt 
create mode 100644 text1.txt 
create mode 100644 text2.txt 
create mode 100644 text3.txt

How to commit changes (and skip the staging area) in Git:

You can add and commit tracked files with a single command by using the -a and -m options.

git  commit  -a  -m  “your  commit  message  here”

root@vmlinux:/var/lib/git/.git/devops1# git status On branch main
Your branch is up to date with 'origin/main'. Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

                          modified:	demo.txt 
                          modified:	info.txt 
                          deleted:	text1.txt
                          renamed:	filename1.txt -> updatefile1.txt 
Untracked files:
(use "git add <file>..." to include in what will be committed)

                          work1 
                          work2 
                          work3
root@vmlinux:/var/lib/git/.git/devops1# git commit -a -m "committing all workfiles"
[main f336f12] commiting all workfiles
files changed, 6 insertions(+), 3 
deletions(-) delete mode 100644 text1.txt
rename filename1.txt => updatefile1.txt (100%)

How to see your commit history in Git:

This command shows the commit history for the current repository:

git log

root@vmlinux:/var/lib/git/.git/devops1# git log
commit 6f76b3faf315ba6b1cfa301fc4ff3f3549af6f93 (HEAD -> main) Author: Venkat Chandra <yvsc369@gmail.com>
Date:	Fri Sep 30 07:51:02 2022 +0000

                  commiting some text files

commit f21baa39ad597977928f8e678abd9b13b3e481f7 (origin/main, origin/HEAD) Author: Venkat Chandra <yvsc.369@hotmail.com>
Date:	Tue Sep 27 12:46:20 2022 +0530

                   added line in demo.txt

commit 8c723ac023528cd3d91404d80a22275628d32433 Author: Venkat Chandra <yvsc.369@hotmail.com>
Date:	Tue Sep 27 12:30:21 2022 +0530

                      info1 renamed to demo

How to see your commit history including changes in Git:

This command shows the commit’s history including all files and their changes

git log -p

root@vmlinux:/var/lib/git/.git/devops1# git log -p
commit 6f76b3faf315ba6b1cfa301fc4ff3f3549af6f93 (HEAD -> main) Author: Venkat Chandra <yvsc369@gmail.com>
Date:	Fri Sep 30 07:51:02 2022 +0000

commiting some text files

diff --git a/5MB.zip b/5MB.zip new file mode 100644
index 0000000..38da09a

Binary files /dev/null and b/5MB.zip differ diff --git a/filename1.txt b/filename1.txt new file mode 100644
index 0000000..38da09a
Binary files /dev/null and b/filename1.txt differ diff --git a/text1.txt b/text1.txt
new file mode 100644 index 0000000..38da09a
Binary files /dev/null and b/text1.txt differ diff --git a/text2.txt b/text2.txt
new file mode 100644 index 0000000..38da09a
Binary files /dev/null and b/text2.txt differ diff --git a/text3.txt b/text3.txt
new file mode 100644 index 0000000..38da09a
Binary files /dev/null and b/text3.txt differ

How to see a specific commit in Git:

This command shows a specific commit.

Replace commit-id with the id of the commit that you find in the commit log after the word commit.

git show commit-id

root@vmlinux:/var/lib/git/.git/devops1# git show

commit f336f12ccff6c48c2ecc11cf00eeafa53432d8ec (HEAD -> main, origin/main, origin/HEAD)
Author: Venkat Chandra <yvsc369@gmail.com>
Date:	Fri Sep 30 13:23:18 2022 +0000

            commiting all workfiles

diff --git a/demo.txt b/demo.txt index 0757846..52228ef 100644
--- a/demo.txt
+++ b/demo.txt

How to see log stats in Git:

This command will cause the Git log to show some statistics about the changes in each commit, including line(s) changed and file names.

git log –stat

root@vmlinux:/var/lib/git/.git/devops1#
commit 6f76b3faf315ba6b1cfa301fc4ff3f3549af6f93 (HEAD -> main) Author: Venkat Chandra <yvsc369@gmail.com>
Date:	Fri Sep 30 07:51:02 2022 +0000

               commiting some text files

5MB.zip | Bin 0 -> 5242880 bytes 
filename1.txt | Bin 0 -> 5242880 bytes 
text1.txt | Bin 0 -> 5242880 bytes 
text2.txt | Bin 0 -> 5242880 bytes 
text3.txt | Bin 0 -> 5242880 bytes
files changed, 0 insertions(+), 0 deletions(-)

How to see changes made before committing them using “diff” in Git:

You can pass a file as a parameter to only see changes on a specific file.

git diff shows only unstaged changes by default.
We can call diff with the –staged flag to see any staged changes.

git diff

git diff all_checks.py git

diff –staged

How to see changes using “git add -p”:

This command opens a prompt and asks if you want to stage changes or not, and includes other options.

git add -p

root@vmlinux:/var/lib/git/.git/devops1# git add -p diff --git a/demo.txt b/demo.txt
index 0757846..52228ef 100644
--- a/demo.txt
+++ b/demo.txt @@ -1,2 +1,3 @@
-i am vc from dba team
-Point1 --Desc ---some dummy data1
\ No newline at end of file
+i am vc from dba team
+i am venkat from JavaHOME
+Point1 --Desc ---some dummy data1 
Stage this hunk [y,n,q,a,d,e,?]? y
<stdin>:10: trailing whitespace. 
i am venkat from JavaHOME
warning: 1 line adds whitespace errors.

diff --git a/info.txt b/info.txt index f6bdfbf..a5a7307 100644
--- a/info.txt
+++ b/info.txt @@ -1,3 +1,5 @@
hello devops1 training started
-by venkat
+info1
+info2
+info3
hello demo1
Stage this hunk [y,n,q,a,d,e,?]? y
<stdin>:10: trailing whitespace. info3
warning: 1 line adds whitespace errors.

How to remove tracked files from the current working tree:

This command expects a commit message to explain why the file was deleted.

git rm filename

root@vmlinux:/var/lib/git/.git/devops1# git rm filename1.txt rm 'filename1.txt'

Verify where the filename1.txt file is deleted or not ?

root@vmlinux:/var/lib/git/.git/devops1# git status 
On branch main
Your branch is up to date with 'origin/main'.

        Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

                          modified:	demo.txt 
                          deleted:	filename1.txt 
                          modified:	info.txt

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

How to rename files in Git:

This command stages the changes, then it expects a commit message

git mv oldfile newfile

root@vmlinux:/var/lib/git/.git/devops1# git mv textt1.txt updatefile1.txt root@vmlinux:/var/lib/git/.git/devops1# git status
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

                        modified:	demo.txt 
                        modified:	info.txt 
                        deleted:	text1.txt
                        renamed:	filename1.txt -> updatefile1.txt

How to ignore files in Git:

Create a gitignore file and commit it.

How to revert unstaged changes in Git:

git checkout filename

How to revert staged changes in Git:

You can use the -p option flag to specify the changes you want to reset.

git reset HEAD

filename git reset HEAD -p

How to amend the most recent commit in Git:

git commit –amend allows you to modify and add changes to the most recent commit.

git commit –amend

!!Note!!: fixing up a local commit with amend is great and you can push it to a shared repository after you’ve fixed it. But you should avoid amending commits that have already been made public.

How to rollback the last commit in Git:

git revert will create a new commit that is the opposite of everything in the given commit. We can revert the latest commit by using the head alias like this:

git revert HEAD

How to rollback an old commit in Git:

You can revert an old commit using its commit id. This opens the editor so you can add a commit message.

git revert comit_id_here

How to create a new branch in Git:

You may also like

Leave a Comment

Contact US

Phone Number

+91 XXXXX XXXXX

Location

2nd floor, # 85, SGR Dental College Rd, Marathahalli, Bengaluru, Karnataka 560037

Email Address

contact@dbacentre.com

    dbacentre.com is a website that publishes practical and helpful out-of-the-box articles for aspirants like you and me. In addition, we seek to present exceptional, remarkable tutorials and tips that modern cloud professionals will appreciate.

     

    Subscribe now

    Edtior's Picks

    by mukilan
    Sql
    by mukilan

    ©2023 Mr DBA CENTRE. All Right Reserved. Designed and Developed by Begintech.in