Tuesday, January 19, 2021

Git - Play around with git


1. Install git

2. Configure git
- Configure your name and email to verify who made change to identify
$ git config --global user.name "Your name"
$ git config --blobal user.email "you@email.com"

3. Create your local repo and initialize it
a. Initialize repo
$ git init
Initialized empty Git repository in /home/sam/mygt/.git/
$ ls -la
drwxrwxr-x.  7 sam sam  119 Jan 19 16:18 .git

.git is created and stores config file

b. Git status
$ git status
On branch master

No commits yet

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

c. Create a file
$ cat >hello.html
Welcome to my page !!!

d. Check the status
$ git status
On branch master

No commits yet

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

        hello.html

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

* you see untracked files.. and you see the name of the file called hello.html

Now, you need to commit (Save it)

You have two steps to commit it.
1. What files you want to add to the staging area. (git add list_of_files)
2. From staging area, you will commit.

$ git add hello.html
[sam@master mygt]$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   hello.html

* see the message output, changes to be committed.

Now, we can make our commit. There are couple of ways to commit
using -m flag followed by the message.


$ git commit -m "Hello file for web page"
[master (root-commit) 061de52] Hello file for web page
 1 file changed, 1 insertion(+)
 create mode 100644 hello.html
[sam@master mygt]$ git status
On branch master
nothing to commit, working tree clean

$ git status
On branch master
nothing to commit, working tree clean

Now, there is nothing to commit.

now, lets run a command git log to see the history of commit

$ git log
commit 061de527aa663d8ee706ce6072971d95c3e86c15 (HEAD -> master)
Author: Jay <sam@gmail.com>
Date:   Tue Jan 19 16:25:57 2021 -0500

    Hello file for web page

Now, I am going to add some more files..

$ touch file{1,2,3}
[sam@master mygt]$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        file1
        file2
        file3

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

$ rm file{1,2,3}
[sam@master mygt]$ git status
On branch master
nothing to commit, working tree clean

$ touch username password contents
[sam@master mygt]$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        contents
        password
        username

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

lets edit hello.html file

Now, I move my files to web server page,

$ sudo cp * /var/www/html/
[sam@master mygt]$ cd /var/www/html/

$ sudo git init
$ sudo git add .
$ sudo git commit -m "files saved at webserver page"

$ rm *
-------------------------------------
Now, start again,
$ cat index.html
<html>
<head> <title> Welcome to my page !!!</title></head>
<body>
        <h1> Welcome to my page </h1>
</body>
</html>

go to browser and see what you can see !!!

Now, create another file file1.js

$ cat file1.js
alert("Hi there !!!")

and update the index file again.

$ cat index.html
<html>
<head> <title> Welcome to my page !!!</title></head>
<body>
        <h1> Welcome to my page </h1>
        <script src="file1.js"></script>

</body>
</html>

now, create a stylesheet file
$ cat style.css

now, include the style and file1 file on index page
Note: stule goes between head open and close tag.
and java script can go between head and also betweeen body tag

$ cat index.html
<html>
<head>
        <title> Welcome to my page !!!</title>
        <link rel="stylesheet" href="style.css">

</head>

<body>
        <h1> Welcome to my page </h1>
        <script src="file1.js"></script>

</body>
</html>

Now, get the IP address and go to the browser and review the page content.



Now, check git status

$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   .htaccess
        new file:   contents
        new file:   hello.html
        new file:   index.html
        new file:   password
        new file:   username

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    contents
        deleted:    hello.html
        modified:   index.html
        deleted:    password
        deleted:    username

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

        file1.js
        style.css

Note: add . will add all the files at one time to the staging area.

$ git add .
[sam@master html]$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   .htaccess
        new file:   file1.js
        new file:   index.html
        new file:   style.css

$ git commit -m "Sample web page created"
[master (root-commit) d7366a6] Sample web page created
 4 files changed, 26 insertions(+)
 create mode 100644 .htaccess
 create mode 100644 file1.js
 create mode 100644 index.html
 create mode 100644 style.css
[sam@master html]$

Now, change the file1 file.
$ vi file1.js
alert("Welcome to the club !!!")

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   file1.js

no changes added to commit (use "git add" and/or "git commit -a")
[sam@master html]$

Now, commit the change
$ git add file1.js
[sam@master html]$ git commit -m "changed the content to file1.js file"
[master f6f0f16] changed the content to file1.js file
 1 file changed, 1 insertion(+), 1 deletion(-)
[sam@master html]$

 Track  your change
$ git log
commit f6f0f16139e47a2c302a2d78cb4aece50458a8d5 (HEAD -> master)
Author: Jay <sam@gmail.com>
Date:   Tue Jan 19 17:10:33 2021 -0500

    changed the content to file1.js file

commit d7366a621d145e2c8b318d770579a1c8e635e783
Author: Jay <sam@gmail.com>
Date:   Tue Jan 19 17:07:29 2021 -0500

    Sample web page created
[sam@master html]$

Now, lets go back to the file1.js file
Look at the hash content,

[sam@master html]$ git checkout f6f0f161
Note: checking out 'f6f0f161'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at f6f0f16 changed the content to file1.js file
[sam@master html]$

Note: key need to be unique if you are not copying all the content of the hash value.

[sam@master html]$ git checkout d7366a621
Previous HEAD position was f6f0f16 changed the content to file1.js file
HEAD is now at d7366a6 Sample web page created
[sam@master html]$ cat file1.js
alert("Hi there !!!")
[sam@master html]$


Now, lets talk about new concept called branch
if you want to keep a clean copy of your code, you can create a new branch
and play around with new features. If you are hally with it, you can merge
or simply destroy.

Note: when you initialize a repository and start making commits, content will be saved on
master branch by default.

-> list the branches
$ git branch
* (HEAD detached at d7366a6)
  master
[sam@master html]$

* you see, tells that you are on different branch.

Run the command git checkout

[sam@master html]$ git branch
* (HEAD detached at d7366a6)
  master
[sam@master html]$ git checkout master
Previous HEAD position was d7366a6 Sample web page created
Switched to branch 'master'
[sam@master html]$ git branch
* master
[sam@master html]$

$ git status
On branch master
nothing to commit, working tree clean
[sam@master html]$ ls
file1.js  index.html  style.css
[sam@master html]$ cat file1.js
alert("Welcome to the club !!!")
[sam@master html]$

If you review the code, you have latest code available on file1.js file.


Lets create a new branch

$ git branch beautiful
[sam@master html]$ git checkout beautifule
error: pathspec 'beautifule' did not match any file(s) known to git.
[sam@master html]$


What we just did is that we copied over all codes to new branch called beautiful.

Now, lets make some modification,

$ cat index.html
<html>
<head>
        <title> Welcome to my page !!!</title>
        <link rel="stylesheet" href="style.css">

</head>

<body>
        <h1> Welcome to my page </h1>
        <script src="file1.js"></script>

        <h3> Life is beautiful </h3>
</body>
</html>

$ cat file1.js
alert("Life is beautiful!!")
[sam@master html]$


$ git branch
  beautiful
* master
[sam@master html]$ git checkout beautiful
M       file1.js
M       index.html
Switched to branch 'beautiful'
[sam@master html]$

check what change you made,
$ git log
commit f6f0f16139e47a2c302a2d78cb4aece50458a8d5 (HEAD -> beautiful, master)
Author: Jay <sam@gmail.com>
Date:   Tue Jan 19 17:10:33 2021 -0500

    changed the content to file1.js file

commit d7366a621d145e2c8b318d770579a1c8e635e783
Author: Jay <sam@gmail.com>
Date:   Tue Jan 19 17:07:29 2021 -0500

    Sample web page created
[sam@master html]$
$ git status
On branch beautiful
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   file1.js
        modified:   index.html

no changes added to commit (use "git add" and/or "git commit -a")
[sam@master html]$ git add .
[sam@master html]$ git status
On branch beautiful
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   file1.js
        modified:   index.html

[sam@master html]$ git commit -m "Modified index and file1 file"
[beautiful 26d6e2d] Modified index and file1 file
 2 files changed, 2 insertions(+), 1 deletion(-)
[sam@master html]$
$ git log
commit 26d6e2d72c55a642beba505db9493f69b3da65a9 (HEAD -> beautiful)
Author: Jay <sam@gmail.com>
Date:   Tue Jan 19 17:32:33 2021 -0500

    Modified index and file1 file

commit f6f0f16139e47a2c302a2d78cb4aece50458a8d5 (master)
Author: Jay <sam@gmail.com>
Date:   Tue Jan 19 17:10:33 2021 -0500

    changed the content to file1.js file

commit d7366a621d145e2c8b318d770579a1c8e635e783
Author: Jay <sam@gmail.com>
Date:   Tue Jan 19 17:07:29 2021 -0500

    Sample web page created
[sam@master html]$

-> Now, check what changes are made

[sam@master html]$ cat file1.js
alert("Life is beautiful!!")
[sam@master html]$ cat index.html
<html>
<head>
        <title> Welcome to my page !!!</title>
        <link rel="stylesheet" href="style.css">

</head>

<body>
        <h1> Welcome to my page </h1>
        <script src="file1.js"></script>

        <h3> Life is beautiful </h3>
</body>
</html>

[sam@master html]$

Now go to master branch
$ git checkout master
Switched to branch 'master'
[sam@master html]$ git branch
  beautiful
* master
[sam@master html]$
[sam@master html]$ cat index.html
<html>
<head>
        <title> Welcome to my page !!!</title>
        <link rel="stylesheet" href="style.css">

</head>

<body>
        <h1> Welcome to my page </h1>
        <script src="file1.js"></script>

</body>
</html>

[sam@master html]$ cat file1.js
alert("Welcome to the club !!!")
[sam@master html]$


see the change made to the files...

$ git log
commit f6f0f16139e47a2c302a2d78cb4aece50458a8d5 (HEAD -> master)
Author: Jay <sam@gmail.com>
Date:   Tue Jan 19 17:10:33 2021 -0500

    changed the content to file1.js file

commit d7366a621d145e2c8b318d770579a1c8e635e783
Author: Jay <sam@gmail.com>
Date:   Tue Jan 19 17:07:29 2021 -0500

    Sample web page created
[sam@master html]$


You don't see the changes you made to beautiful branch.


Now, whatever you made changes is not on master branch.
Your beautiful branch is still beautiful.


Now, lets see, whatever you made changes to beautiful repo is perfect
how do you merge these two repos together?

So, you can use with merge command flag along with the branch you want to merge.

$ git branch
  beautiful
* master
[sam@master html]$ git merge beautiful
Updating f6f0f16..26d6e2d
Fast-forward
 file1.js   | 2 +-
 index.html | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
[sam@master html]$

see, how many files changed and how many deleted..

check the log, you see all the commit you made to beautiful are here.
$ git log
commit 26d6e2d72c55a642beba505db9493f69b3da65a9 (HEAD -> master, beautiful)
Author: Jay <sam@gmail.com>
Date:   Tue Jan 19 17:32:33 2021 -0500

    Modified index and file1 file

commit f6f0f16139e47a2c302a2d78cb4aece50458a8d5
Author: Jay <sam@gmail.com>
Date:   Tue Jan 19 17:10:33 2021 -0500

    changed the content to file1.js file

commit d7366a621d145e2c8b318d770579a1c8e635e783
Author: Jay <sam@gmail.com>
Date:   Tue Jan 19 17:07:29 2021 -0500

    Sample web page created
$ git status
On branch master
nothing to commit, working tree clean
[sam@master html]$ git branch
  beautiful
* master
[sam@master html]$

Now, you have same contents on your master branch

$ cat file1.js
alert("Life is beautiful!!")
[sam@master html]$ cat index.html
<html>
<head>
        <title> Welcome to my page !!!</title>
        <link rel="stylesheet" href="style.css">

</head>

<body>
        <h1> Welcome to my page </h1>
        <script src="file1.js"></script>

        <h3> Life is beautiful </h3>
</body>
</html>

[sam@master html]$



No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...