The Git story: Add,commit,delete commands

In this blog, we will see git in details. Git has three stages:
-Working dir
-Staging
-Repository

Whenever we create a new file it would be part of working dir. We need to add this file in staging.Post staging we need to commit to store changes in repository

– Initial stage of working dir
[root@mytest amit]#
[root@mytest amit]# git status
# On branch master
nothing to commit (working directory clean)
[root@mytest amit]#

-Created two file test2.sh and test3.sh in working dir
[root@mytest amit]# git status
# On branch master
# Untracked files:
# (use “git add <file>…” to include in what will be committed)
#
# test2.sh
# test3.sh
nothing added to commit but untracked files present (use “git add” to track)

-Adding test2.sh in staging and commit
[root@mytest amit]#
[root@mytest amit]# git add test2.sh
[root@mytest amit]# git status
# On branch master
# Changes to be committed:
# (use “git reset HEAD <file>…” to unstage)
#
# new file: test2.sh
#
# Untracked files:
# (use “git add <file>…” to include in what will be committed)
#
# test3.sh

#git commit -m “second file”
[master 8a31c70] Second file
Committer: Amit <amit@openwriteup.com>
1 files changed, 2 insertions(+), 0 deletions(-)
create mode 100644 test2.sh

Want to check diff between files version

git diff
[root@mytest amit]# git diff
diff –git a/test.sh b/test.sh
index 7a870bd..2b56848 100644
— a/test.sh
+++ b/test.sh
@@ -1,2 +1,2 @@
#!/bin/bash
-echo “Hello World”
+echo “i theHello World”

1 #!/bin/bash
2 echo “i theHello World”

If file is added in staging ,it will not show in diff.

[root@mytest amit]# git diff
[root@mytest amit]# git diff –staged
diff –git a/test.sh b/test.sh
index 7a870bd..2b56848 100644
— a/test.sh
+++ b/test.sh
@@ -1,2 +1,2 @@
#!/bin/bash
-echo “Hello World”
+echo “i theHello World”

git to Delete file:
git rm test.sh
rm ‘test.sh’

[root@mytest amit]# git status
# On branch master
# Changes to be committed:
# (use “git reset HEAD <file>…” to unstage)
#
# deleted: test.sh
#

root@mytest amit]# git commit -m “deleted”
[master ab81bbb] deleted
Committer: Amit <amit@openwriteup.com>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
1 files changed, 0 insertions(+), 2 deletions(-)
delete mode 100644 test.sh
[root@mytest amit]# git status
# On branch master
nothing to commit (working directory clean)

Renaming the file in Git
[root@mytest amit]# git mv test2.sh test4.sh
[root@mytest amit]# git status
# On branch master
# Changes to be committed:
# (use “git reset HEAD <file>…” to unstage)
#
# renamed: test2.sh -> test4.sh
#
[root@mytest amit]# git commit -m “renamed”
[master 1c8445b] renamed
Committer: Amit <amit@openwriteup.com>1 files changed, 0 insertions(+), 0 deletions(-)
rename test2.sh => test4.sh (100%)
[root@mytest amit]# git status
# On branch master
nothing to commit (working directory clean)

amit
Working as an Architect, Infra designer. If any query related to Infra Design [related to cloud,vmware,openstack], please write: amit@openwriteup.com