Messing up with git
Git has three states:
- Committed
- Modified
- Staged
Committed: - The date is safely stored on your local repo
Modified: You made change to the file but not yet saved (comitted) to your local database.
Staged: You made sure that your modified version is ready to go into next commit state.
For eg,
You have a working directory
- you made change to your file, that means, you modified the file.
- Now, you have to staged it, that mean you have to bring it to staging area.
- Once the file in staged area, you verify that everything looks good, then you commit it to save it to local repo
-
so,
working area staging area repo
modify file -> stage it -> commit to save it to repo.
.git is your database to track your change.
=========================================
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]$
-----------------------------------------------
In Summary
1. Create a directory and make it as a git repo
# mkdir myrepo; git init
git init will create a new repo.
Ok, lets about .git file
- this is a config file stores the config info.
We created local repo or initialize a repo.
Now, we add content, modify the content here.
Three are three stages on this modification and saving process.
1. Working Directory (Working dir)
2. Stading area (Index area)
3. Committed area. (Saved or HEAD)
OK, working area holds real file and the index is the staging area and
committed area is the one where you saved your file
2. Clone a repo from github
$ git clone https://github.com/myrepo123
Basically you copy the content from remote repo to your local repo on your PC.
3. Now, edit one of a file and save the file
Lets say you edited a file called hello.txt, modified and saved.
$ cat >> hello.txt
Added new contents
CTRL+D -> to save it
4. Now run the git status and add to the staging area
$ git status
it will complain you that its not added to staging area.
$ git add <filename> or < * > or < . >
5. Now you have to same to the committed area.
$ git commit -m "Your Message"
Finally file is saved on your repo.
But you have to push your repo to remote location so that other folks can see the change you made.
6. Push to your remote repo.
$ git push origin master
master is the name of the branch. Most of the case, its master, if you have changed, use it.
or push it to your repo by first adding to the list
$ git remote add origin <server>
1. Get remove copy of the code on your local computer
$ git clone <repo_URL>
-> Remote Repo/Remote location -> clone/copy -> to local repo (PC)
2. Check what branch you are working on
$ git branch
by default master and it is a local remo
3. Create a new branch
$ git branch newbranch
basically it is an exact copy of master branch.
So, you have two branches.
Note: Until this point, size of the directory does not change
since its just reference point to the origina.
There is a directory called .git, which keep tracks of the changes. In
depth, it stores the difference of diff command, storing information about
new branch.
3. Change between branches
$ git checkout <branch_name>
$ git checkout master
$ git checkout newbranch
Here, you will modify files and add new features. you save it.
Now, you save your file but now, you have to add this file to staging area.
You want to keep track of changes you made. You add file to staging area to
keep track of changes. To add to stading area
$ git add <file_name>
$ git add hello.txt
4. Git status command
- Shows current status of the branch.
- It also shows information regarding what changes are staged that are going to be
part of next commit.
5. Commit (Save) the change
$ git commit -m "Finally saved !!!"
Lets say, you modified files on your newbranch. Yo uverify all the changes and you
want to merge with your original barnch - master branch.
6. merge branches
$ git merge <branch_name>
We have to commit the change to our master branch so that we can push our change
to remove branch.
To merge new branch to master branch,
1. First checkout to master barnch
$ git checkout master
2. And initiate a merge process.
$ git merge newbranch
7. Now, upload your code to remote repo
$ git pish origin <branch-name>
$ git push origin master
so far what we did is
- clone a repo
- create a branch
- change the files and add festures
- stage the change
- Commit the change
- Merge the change to master branch
- Push the change to the remote repo.
===============================
1. Install
2. Config
$ git config --global user.name "Yourname"
$ git config --global user.email YourEmail@email.com
3. Initialize
$ git init
4. Download code from remote repo
$ git clone <Remote_URL>
github.com/csinghdev
5. Edit a file
$ vi abc.html
<html><body>
<h1> Welcome to git tutorial </h1>
<p> Learning is not always fun </P>
</body></html>
6. Add it to staging area
$ git add -A
$ git status
changes are shown here.
it will show no commits are made yet.
7. Commit (Save) the change on the file to the staging area
$ git commit -m "Saved, big time !!!"
$ git status
Will display, nothing to save.
8. Track the change
$ git log
it will show all commits with unique commit ID.
Using these ID, you can go back.
9. Now, lets make changes to the file.
<html><body>
<h1> Welcome to git tutorial </h1>
<p> Learning is not always fun </P>
<h3> Life is beautiful !!! </h3>
</body></html
10. Now, run the status command and add it to stage area
$ git status
$ git add -A ( or * or . )
$ git status
It will show changes to be committed which mean, you ahve to save/commit
11. Commit the change
$ git commit -m "Modified - committed - 2nd time"
$ git status -> check the status
$ git log -> see the no. of time changes are mode
12. Now, lets change your file again,
<html><body>
<h3> Life is beautiful !!! </h3>
</body></html
13. Check the status and commit the change
$ git status
$ git add -A
$ git status
$ git commit -m "Third commit)
14. Now, you realize that you made a mistake and want to go to previous version
$ git log
will displays the commit ID
To restore to previous, just copy the hash value of commited ID, you can get your original file.
$ git checkout <COMMIT ID - HASH value>
$ cat abc.html
You will all file contents here.
$ git log
Create a new branch
$ git checkout -b newbranch
$ git branch -> list new branch
$ git checkout master
$ git merge newbranch
$ git log
now, your new branch is merged with master branch.
Now, lets create a repo on git hub. Browse, login and create a new private repo
Once you create a repo, it will give your the repo link with comand lie,
$ git remove add origin https://github.com/myrepo.../test.git
$ git push origin master
Now, your codes are pushed to remove repo
Now, say, if its your repo and need to share with your peers,
Go to github -> settings -> Manage access -> Invite collaborator -> enter usename or email.
Pull the updated repo
$ git pull origin master
Concept of branches
- You copy the code on your local computer
- You make a local copy of copied contents.
# git branch <name_of_branch>
Check what branch you are working on
# git branch
If you see master, it is an exact copy of remote repo
to create, you run the command
# git branch update_1
# git branch
3. to switch to the branch
# git checkout <name_of_branch>
4. add changed files to the staging area
# git add <file-names>
# git add .
5. show current state of the branch. Whar are staged and what are unstaged.
# git status
6. Now, save it to local repo
# git commit -m "My commit messages"
7. Now, merge it to local master branch
# git checkout master
# git merge <name_of_branch> # the branch you created and now merging to master
8. Now, sync to remote repo
# git push origin <brnch-name>
# git push origin master
origin is repote repo
master is local repo
install, configure,
initilize
git init
create file
hello.txt
git add hello.txt -> stage ->
git commit -m "Your message"
version of file now git will track
git log
git add and git commit to save
it will keep record of all changes
git status
git diff
git reset
git branch
git checkout
so, in summary, what we did,
1. Clone a repo
2. Created a branch
3. Modified the content in the branch
4. Saved (commit) the change
5. Merged to master
6. Pushed the change to the repote repo.
1. Set up password less authentication between servers.
$ ssh-keygen
$ cat id_rsa.pub
2. Copy the key and paste it to github/gitlab
3. Go to your home dir and create a dir
$ mkdir myrepo
4. Clone remote repo
$ git clone <repo_URL>
5. Start working on it by editing files
$ git clone <URL_Repo>
$ git checkout -b task_2254
$ git status
$ git add roles
$ git status
$ git commit
$ git status
$ git push origin task_2254
$ git status
$ git checkout master
$ git pull --rebase
$ git status
$ git remote -v
$ git branch -a
$ git branch -d -r origin/task_2254
$ git git branch -d task_2254
$ git branch -a
$ git status
$ git log
$ git pull <URL>
$ ansible -i patch_hosts all -a "uptime"
No comments:
Post a Comment