lab2
lab2
configuration management(SCM):
Note:
1) commit and checkout operations will be performed between workspace and repository.
work space —>commit—--> Repository
Repository—>checkout —->workspace
GIT Architecture:
❖ Now cse directory acts as a working directory. We have to request git, to provide
version control for this directory. For this we have to use the git init command.
git init
❖ Once we create a workspace, if we want version control, then we require a local
repository. To create that local repository we have to use the git init command.
❖ .git is an empty repository, which is a hidden directory.
Note:
1) If our working directory contains any files, then these files won't be added to the local
repository by default, we have to add explicitly.
2) If our working directory already contains local repository(.git), still if we call git init
command, then there is no impact.
Creating Files with some Content and adding to staging Area and then commit:
git status
It shows the current status of all files in each area, like which files are untracked, which are
modified, which are staged etc.
We can get concise information by using -s option.
Syntax: git status -s
* After writing the content, press ctrl + D to get back to the command prompt.
git add
To add files from the working directory to the staging area for tracking/commiting purposes, we
have to use the git add command.
To add all files present in current working directory
git add .
To add one or more specified files
git add a.txt
git add a.txt b.txt
Even we can use pattern also
git add *.txt
git add *.java
git config
Git Configurations before 1st Commit:
Before the first commit, we have to configure username and mail id, so that git can use this
information in the commit records. We can perform these configurations with the following
commands.
global means these configurations are applicable for all repositories created by git. If we are not
using global then it is applicable only for the current repository.
We can change user name and mail id with the same commands
git commit
If we want to commit staged changes, then we have to use the git commit command. For every
commit, a unique commit id will be generated. It is of 40-length hexadecimal string.
While using git commit command, commit message is mandatory.
git log
It shows the history of all commits.
It provides commit id, author name,maild , timestamp and commit message.
Adding these modified Files to the staging Area and then commit:
ls:
This command will list out all files present in workspace
git diff command
It is a very common requirement to find differences between the content of a particular
file or all files.
1) Between working directory and staging area
2) Between working directory and last commit
3) Between staged area and last commit
4) Between working directory and a particular commit
5) Between staged area and a particular commit
6) Between two specific commits
For this we are required to use the git diff command. diff means difference
Example:
Create file1.txt file2.txt and add some content.
Type vim file.txt in command type
After executing the above command,the Text editor will open.
To insert data, type i in the keyword .
Enter the data you want to.
Press esc and type :wq(in editor itself) to come out of the vim file
file1.txt
First line in file1.txt
Second line in file1.txt
file2.txt
First line in file2.txt
Second line in file2.txt
file2.txt
First line in file2.txt
Second line in file2.txt
Third line in file2.txt
Fourth line in file2.txt
Now commit both files i.e 2 files contain 4 lines of content.
Now add one line to the file1.txt
file1.txt
First line in file1.txt
Second line in file1.txt
Third line in file1.txt
Fourth line in file1.txt
Fifth line in file1.txt
file1.txt
First line in file1.txt
Second line in file1.txt
Third line in file1.txt
Fourth line in file1.txt
Fifth line in file1.txt
sixth line in file1.txt
1. To see the difference in File Content between Working Directory and staging Area
--- a/file1.txt
--- means missing lines in staged copy
+++ b/file1.txt
+++ means new lines added in working directory version
@@ -3,3 +3,4 @@
-3,3
- means source version from 3rd line onwards total 3 lines
+3,4
+ means destination version from 3rd line onwards total 4 lines
If any line prefixed with space means it is unchanged.
If any line prefixed with + means it is added in destination copy.
If any line prefixed with - means it is removed in destination copy.
@@ -3,3 +3,4 @@
Second line in file1.txt
Third line in file1.txt
Fourth line in file1.txt
Fifth line in file1.txt
+sixth line in file1.txt
Clear indication that one line added in the working directory copy when compared with staged
copy.
+sixth line in file1.txt
2. To see the difference in File Content between Working Directory and Last Commit
The last commit can be referenced by HEAD.
git diff HEAD file1.txt
It shows the differences between working copy and last commit copy.
3. To see the difference in File Content between staged Copy and Last Commit
We have to use --staged option or --cached option.
git diff --staged HEAD file1.txt
It shows the differences between staged copy and last commit copy.
Here HEAD is optional. Hence the following 2 commands will produce same output
git diff --staged HEAD file1.txt
git diff --staged file1.txt
4. To see the difference in File Content between specific Commit and Working Directory
Copy.
git diff 7chracters_of_specified_commitid filename
$ git log --oneline
042b184 (HEAD -> master) 2 files added and each file contains 4 lines
9b64469 2 files added and each file contains 2 lines
Summary:
git diff
Shows the differences in the content of working directory, staging area and local repository. we
can use in the following ways
1) git diff file1.txt To compare working directory copy with staged copy
2) git diff HEAD file1.txt To compare working directory copy with last commit copy
3) git diff --staged file1.txt
git diff --cached file1.txt
git diff --staged HEAD file1.txt git diff --cached HEAD file1.txt
To compare staged copy with last commit copy
4) git diff <commit id>file1.txt To compare working directory copy with the specified commit
copy.
5) git diff --staged <commit id>file1.txt To compare staged copy with the specified commit
copy.
6) git diff <source commit id> <destination commit id>file1.txt To compare content in the
file between two commits
7) git diff HEAD HEAD~1 file1.txt To compare content in the file between last commit and
last but one commit.
8) git diff <source commit id> <destination commit id> : To compare content of all files
between two commits.
9) git diff master test It shows all differences between master branch and test branch
10) git diff master origin/master It shows all differences between master branch in local
repository and master branch in remote repository.
git restore command
This command can be used to undo the effects of git add and unstage changes you have
previously added to the Staging Area.
This restore command can also be used to discard local changes in a file, thereby restoring its
last committed state.
Example:
Open Git Bash , add the file to the staging area and see the status.
git restore –staged:
Removes the file from the Staging Area, but leaves its actual modifications untouched
git restore:
Suppose you made changes to a file named example.txt, but now you want to discard those
changes.
Open b.txt , add second line,add to the staging area and then commit
git restore --source:
This command is specifically designed to restore files in your working directory from a
specific commit.