top
upGrad KnowledgeHut SkillFest Sale!

Search

GIT Tutorial

1. Getting started with git - Git configuration Git provides configuration file to set the initial parameters for the repository. There are 3 levels of configurations:System-level configuration: applies to the entire computer that it is installed onUser-level configuration: for a particular userRepository-level configuration: Project levelLevels:SystemUserProjectConfig file location:/etc/gitconfig~/.gitconfigProject/.git/configList the configurationsgit config --system --listgit config --global --listgit config --listEdit the configuration:git config --system --editgit config --global --editgit config --editBelow are the git basic commands used to set the configurations:To set the levels of the configurations use the respective flags (--system, --global, none)--Set the username for a logged in user, applicable to all the repositoriesgit config --global user.name "name"--Set the password for the logged in user, applicable to all the repositoriesgit config --global user.password “yourpasswordString” git config --global user.email "email"--Set any editor of your preference as a system wide settinggit config --system core.editor "vim"--Set the default tool as ‘vimdiff’ to be used for merging branches for only a projectcd ~/project git config merge.tool "vimdiff"--set a unique username for a projectgit config user.name “user1”--set a unique password for the user of this projectgit config user.password “user1@123”Add colors and show git branches in bash terminal edit the .bash_profile file:vi ~/.bash_profile parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ [\1]/' } export PS1="\u@\h:\W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $"2. The Git workflowThe best git workflow moves the source code from working directory to the Staging area to the local repository and then publishes it on the remote repository maintaining data integrity and tracking every bit of data change.a) Create a local git repository#Initialize your project to be a git project using the “init” commandThe command create or initialize a new repository and places “.git” directory in the project root with metadata for the repo.cd proj_workFlow echo "Readme file." > README.file git init .Initialized empty Git repository in /Users/Divya1/myRepos/proj_workFlow/.git/b) Add files/directories in the working directory  c) View the 3 git stages (The git status command) git statusOn branch master No commits yet Untracked files:  (use "git add <file>..." to include in what will be committed) README.file codes/ temp/ nothing added to commit but untracked files present (use "git add" to track)d) Stage the files (The git add command): Add files/dirs. to the staging areaAdds a snapshot of the working area to the staging area scheduled for the next commit.Usage: git add --all git add . git add -AStep 1: Add one file alonegit add READMEStep 2: Check the status$git status On branch master No commits yet Changes to be committed:  (use "git rm --cached <file>..." to unstage)         new file:   README.file Untracked files:  (use "git add <file>..." to include in what will be committed) codes/ temp/Step 3: Add all the files git add --all OR git add --A OR git add . git statusOn branch master No commits yet Changes to be committed:  (use "git rm --cached <file>..." to unstage) new file:   README.files new file:   codess/mycode.sh new file:   temp/file.tmpStep 4: Modify the README file again and see the status echo “version:1.0”>>README git statusChanges to be committed:  (use "git rm --cached <file>..." to unstage) new file:   README.file new file:   codes/mycode.sh new file:   temp/file.tmp 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:   README.fileNOTE: Part of the file is staged and part of it with the new changes are not staged. If you commit this snapshot, the first edit of the README file will be saved in the repository; however the later edit still remains in the working directory alone.Run git add again to stage the file.git add . git statusOn branch master No commits yet Changes to be committed:  (use "git rm --cached <file>..." to unstage) new file:   README.file new file:   codes/mycode.sh new file:   temp/file.tmpe) Commit the changes to local repocommits the staged snapshot to the project history. Only tracked & staged files are committed.git commit -m “Initial commit” [master (root-commit) bd5982e] Initial commit  3 files changed, 7 insertions(+)  create mode 100644 README.file  create mode 100644 codes/mycode.sh  create mode 100644 temp/file.tmpA good commit message:Commit c276e4ae3fec434209c133c03d82849bcac99e57 ( HEAD -> master )Author: divya < >Date: Wed Dec 13 23:22:32 2017 -0800Issue#901: Check for any missing tables in “production” database and report  This script connects to the “production” database and produce a list of all “tables”.Issue#901 referred to some of “production” tables missing after certain CRUD operation on Database.A list of “prod” tables have been gathered; against which the present table names will be verified.The script makes a connection to the “production” database reading the db_config file to get the username, password and database name.The script then reads all the objects of type “table” and saves the table names in a file which is compared against the expected table names list.If there is mis-match in both the files, the script generates a warning in the warning message file as well as sends out mail to the respective email id.Usage: <script_name> <db_config> <db_name> <expectedTables_list_path> Reported-by: whoever-reported-it Signed-off-by: Your name <youremail@yourhost.com>Diagram: Single commit repo data  Diagram: Git object data for multiple commits Stage changes as multiple commits: breaking commits into logical units by committing separate commits.echo "bugfix#1234">>codes/mycode.sh echo "Project: git workflow">>README.file$git status On branch master No commits yet Changes to be committed:   (use "git rm --cached <file>..." to unstage) modified:   README.file modified:   codes/mycode.shgit add code/code.sh git commit -m "Add code for bug#1234" git add README.file git commit --m "Add project info"View history and diffsView project history (The ‘git log’ command)Displays committed snapshots, you can filter or search for specific changes.View history and diffsView project history (The ‘git log’ command)Displays committed snapshots, you can filter or search for specific changes.3. View history and diffsa)View project history (The ‘git log’ command)Displays committed snapshots, you can filter or search for specific changes.Project: proj_workFlowcommit 1e003594c6egit log commit 1e003594c6e0f8850ec6d4fb9507f38e1ee80583 (HEAD -> master) Author: divya bhushan <divya@developer.com> Date:   Fri Nov 16 15:20:43 2018 +0530    Add project infocommit d8f90e5e9949a43291f6206f522791c14bdda460 Author: divya bhushan <divya@developer.com> Date:   Fri Nov 16 15:20:21 2018 +0530   Add code for bug#1234commit e6f82b5eb88863fb2d811ff5b2a1e18e76246bf4 Author: divya bhushan <divya@developer.com> Date:   Fri Nov 16 15:17:01 2018 +0530   Initial commitProject: myProjLog Example (1): git logcommit 3fbfa9a23bd7eac05226e08f40c0cb8138493619 (HEAD -> master) Author: divya <divya@developer.com> Date:   Wed Aug 22 16:53:26 2018 +0530   Create the initial project structureLog Example (2): Limiting the output to single line commit messagegit log --oneline 3fbfa9a (HEAD -> master) Create the initial project structureLog Example (3): Limit the log output to n number of commitsgit log -n2 or git log -2 git log -5 --oneline 9ecf5bd (HEAD -> master, myProj/master) Adding admin tasks. fb5d4ee Merge branch 'master' of https://github.com/greets/myProj 34a4e65 Update README to version 3.3 43d4441 (tag: new, origin/master, origin/HEAD) Update README to version 3.2 0f491ba (origin/issue) Merge branch 'master' of https://github.com/greets/myProj#To set aliases and create your own powerful and more resourceful commands:1. Use alias “mylog” to display all logs in graphical one-liner format.git config --global alias.mylog "log --graph --all --oneline"2. Set the alias “hist” to print all the commits in pretty one-liner color coded formatgit config --global alias.hist 'log --pretty=format:"%C(yellow)%h%Creset %ad | %C(green)%s%Creset%C(red)%d%Creset %C(blue)[%an]" --graph --decorate --date=short' %h: abbreviated commit hash %ad: author date %s: subject %d: ref names, like the --decorate options of git log %an: author nameLog Example (4): Search for commits by a particular authorgit hist --author="administrator"ORgit log --author="administrator" --oneline ce0341f added featureB 757ca44 modified temp.tmp file c01d07a added featureB 3aec5b7 added temp info from admin user 13d556e added temp info 33b6e04 modified db.log file as admin userLog Example (5): Include which files were altered and the relative number of lines that were added or deleted from each of them.git log --stat  git log --n 3 --stat commit fb5d4eeb0a9be6a0e0c084ba51fd0308529c343e (HEAD -> master) Merge: 34a4e65 43d4441 Author: divya <divya@developer.com> Date:   Wed Aug 22 12:17:19 2018 +0530    Merge branch 'master' of https://github.com/greets/myProjcommit 34a4e658489c99a2c42d1edf14c7c1d695df131a Author: greets <developer2@github.com> Date:   Mon Dec 18 04:53:04 2017 -0800    Update README to version 3.3 README.md | 1 + 1 file changed, 1 insertion(+) commit 43d4441bb60e4dc28ce07aaa2d4c37e8416eb061 (tag: new, origin/master, origin/HEAD) Author: greets <developer2@github.com> Date:   Mon Dec 18 04:53:04 2017 -0800    Update README to version 3.2 README.md | 1 + 1 file changed, 1 insertion(+)Log Example (6): Shows patch difference or full diff of each commitgit log --p git log --n 2 --pcommit fb5d4eeb0a9be6a0e0c084ba51fd0308529c343e (HEAD -> master) Merge: 34a4e65 43d4441 Author: divya <divya@developer.com> Date:   Wed Aug 22 12:17:19 2018 +0530 Merge branch 'master' of https://github.com/greets/myProjcommit 34a4e658489c99a2c42d1edf14c7c1d695df131a Author: greets <developer2@github.com> Date:   Mon Dec 18 04:53:04 2017 -0800    Update README to version 3.3diff --git a/README.md b/README.md index c6f6cd4..14b4f8c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # myProj +#Version 3.3This Project is created to get a good hands-on for the git development. You could edit this project locally working on your machine.Log Example (7): Search for commits with a commit message that matches the grep patterngit log --grep="hotfix" git log --oneline --grep="iss53" 078f9f5 Merged iss53 into master after resolving conflict ab3a5e5 Modified editInfo from iss53 branch b777b16 modified editInfo from iss53 branchLog Example (8): See if there’s any commits that have not been pushed to your origin remote.#read this as: show the commits missing in origin, present in HEADgit log origin..HEAD      commit fb5d4eeb0a9be6a0e0c084ba51fd0308529c343e (HEAD -> master) Merge: 34a4e65 43d4441 Author: divya <divya@developer.com> Date:   Wed Aug 22 12:17:19 2018 +0530     Merge branch 'master' of https://github.com/greets/myProjcommit 34a4e658489c99a2c42d1edf14c7c1d695df131aAuthor: greets <developer2@github.com>  Date:   Mon Dec 18 04:53:04 2017 -0800     Update README to version 3.3Log Example (9): Show commit logs missing in master, present in ‘topicBranch’  branchgit log --oneline master..topicBranch commit 5d1e3baa9b790e3ce46cfc48499177c895ddbfb6 (topicBranch) Author: divya <divya@developer.com> Date:   Thu Sep 6 14:47:11 2018 +0530     added runtest.t from featureB_old branch.Log Example (10): commits that affected only a single file git log <file> git log .gitignorecommit db263a6d1968d917c36688c34263980de591c040 Author: divya <divya@developer.com> Date:   Sat Feb 28 12:38:28 2015 +0530    pattern change in .gitignore filecommit fa676058abbfe61cb751d315a9942200e180020f Author: divya <divya@developer.com> Date:   Sat Feb 28 10:30:33 2015 +0530    add the local .gitignore fileLog Example (11): logs after, before, after-before a period git log --after=”2015-02-27” -n2 git log --before=”2015-02-27” git log --after="2017-12-01" --before="2018-07-01"Log Example (12): Logs since a certain periodgit log --since=”1 week ago” git log --since=”yesterday” git log --since=2.weeks -commit made in last 2 weeks git log --since=1.day - commit made in last 1 day git log --since=30.minutes - commit made in last 30 minutes git log --since=5.hours - commit made in last 5 hours git log --since=5.hours --pretty=onelineLog Example (13): 38e1ee80583 (HEAD -> mgit log <since>..<until> git log --since=4.months --until=2.weeks git log --since="1 week ago" --until="yesterday"Log Example (14): Display the logs between the commits with commit Idsgit log --oneline 8b92ffc.. 43d4441 43d4441 (tag: new, origin/master, origin/HEAD) Update README to version 3.2 0f491ba (origin/issue) Merge branch 'master' of https://github.com/greets/myProj 904ce2b Create README file b) View changes between the 3 Tier of git(The ‘git diff’ command)This command helps you to see the difference between Working directory, Staging area and Local repository.There will be no output when the 2 tiers/layers have the same version of the code.Diagram: ‘git diff’ between 3 layers of git workflowgit diff git diff --cached  git diff HEADProject: proj_workFlowConclusion:This command makes no change in either of the working dir, Index or local repo.It only shows the differences between the 3 layers and how your code flows.‘git diff’ gives you an opportunity to review your code every time before you:- edit your code in working directory- add your code to the staging area(index)- commit the code to local repository4. Delete, rename and move files/directoriesGit provide “git” commands to delete, rename and move files and directories.git rm: Remove files from the working tree and from the index. Unlike ‘mv’ command ‘git rm’ will not remove a file from just your working directory.git mv: Move or rename a file, a directory, or a symlink both in Working directory and Index.cd proj_workFlow git mv README.file README git rm codes/mycode.sh git mv temp/file.tmp . git statusOn branch initialBranch Changes to be committed:   (use "git reset HEAD <file>..." to unstage)           renamed:    README.file -> README deleted:    codes/mycode.sh renamed:    temp/file.tmp -> file.tmp5. Ignore file typesIn certain cases you might need to ignore certain file types from being tracked and uploaded in the repository, such as compiled sources (.class, .dll..), Database log files(.sql, .log), packages(.7z, .gz, .jar, .tar etc), temporary OS generated files etc.Git provides “.gitignore” file wherein you can mention the regex format for the filetypes you want git to ignore.a) Create a local .gitignore file: vi .gitignoreDefine a single “.gitignore” file in the root directory of the project.Only meant for a specific project repositoryThis file has to be staged and committed every time there is an edit.This file is shared among different users and repositories upon clone and pull/push.b) Create a global .gitignoreCreate a file under home directory of the uservi ~/.gitignore_globalDefine the file path under global config file.git config --global core.excludesfile ~/.gitignore_globalThe rules listed here will be applied to all the repositories on the local computerThis file will not be versioned and not distributed upon clone or pull/push with remote.c) Repository personal ignore file on local computerThis file is not versioned and not distributed with your repository, i.e. not shared with other users.vi .git/info/exclude#Sample ignore patterns:vi .gitignore vi ~/.gitignore_global vi .git/info/exclude *.tmp -- ignore all files ending with tmp extension *.[oa] -- all files ending with extension ‘o’ and ‘a’ *.exe -- ignore all files ending with exe [tT]emp*.[oa] -- all files starting with temp or Temp and ending with an ‘o’ or ‘a’ [aA-zZ0-9]* /test* -- Ignore filenames that start with a test - forward slash means beginning of path temp/**/* -- Ignore all files under temp directory recursively temp/**/*.tmp -- Ignore only *.tmp extension files under temp directory recursively temp/*.tmp -- Ignore *.tmp files only under temp directory and not sub-directories temp/* -- Ignore all files under dir temp
logo

GIT Tutorial

Working locally with git

1. Getting started with git - Git configuration 

Git provides configuration file to set the initial parameters for the repository. There are 3 levels of configurations:

  • System-level configuration: applies to the entire computer that it is installed on
  • User-level configuration: for a particular user
  • Repository-level configuration: Project level
Levels:SystemUserProject




Config file location:/etc/gitconfig~/.gitconfigProject/.git/config
List the configurationsgit config --system --listgit config --global --listgit config --list
Edit the configuration:git config --system --editgit config --global --editgit config --edit

Below are the git basic commands used to set the configurations:

To set the levels of the configurations use the respective flags (--system, --global, none)

--Set the username for a logged in user, applicable to all the repositories

git config --global user.name "name"

--Set the password for the logged in user, applicable to all the repositories

git config --global user.password “yourpasswordString”
git config --global user.email "email"

--Set any editor of your preference as a system wide setting

git config --system core.editor "vim"

--Set the default tool as ‘vimdiff’ to be used for merging branches for only a project

cd ~/project
git config merge.tool "vimdiff"

--set a unique username for a project

git config user.name “user1”

--set a unique password for the user of this project

git config user.password “user1@123”

Add colors and show git branches in bash terminal edit the .bash_profile file:

vi ~/.bash_profile
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ [\1]/'
}

export PS1="\u@\h:\W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $"

2. The Git workflow

The best git workflow moves the source code from working directory to the Staging area to the local repository and then publishes it on the remote repository maintaining data integrity and tracking every bit of data change.

a) Create a local git repository

#Initialize your project to be a git project using the “init” command

The command create or initialize a new repository and places “.git” directory in the project root with metadata for the repo.

cd proj_workFlow
echo "Readme file." > README.file

git init .

Initialized empty Git repository in /Users/Divya1/myRepos/proj_workFlow/.git/

Create a local git repository

b) Add files/directories in the working directory 

 

c) View the 3 git stages (The git status command) 


git status

On branch master
No commits yet

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

README.file
codes/
temp/

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

d) Stage the files (The git add command): Add files/dirs. to the staging area

  • Adds a snapshot of the working area to the staging area scheduled for the next commit.
Usage:
git add --all
git add .
git add -A

Step 1: Add one file alone

git add README

Step 2: Check the status

$git status
On branch master
No commits yet

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

         new file:   README.file

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

codes/
temp/

Step 3: Add all the files

 git add --all
OR
git add --A
OR
git add .

git status

On branch master
No commits yet

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

new file:   README.files
new file:   codess/mycode.sh
new file:   temp/file.tmp

Step 4: Modify the README file again and see the status


echo “version:1.0”>>README

git status

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

new file:   README.file
new file:   codes/mycode.sh
new file:   temp/file.tmp
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:   README.file

NOTE: Part of the file is staged and part of it with the new changes are not staged. If you commit this snapshot, the first edit of the README file will be saved in the repository; however the later edit still remains in the working directory alone.

Run git add again to stage the file.

git add .
git status

On branch master

No commits yet

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

new file:   README.file
new file:   codes/mycode.sh
new file:   temp/file.tmp

e) Commit the changes to local repo

  • commits the staged snapshot to the project history. Only tracked & staged files are committed.

git commit -m “Initial commit”

[master (root-commit) bd5982e] Initial commit
 3 files changed, 7 insertions(+)
 create mode 100644 README.file
 create mode 100644 codes/mycode.sh
 create mode 100644 temp/file.tmp

A good commit message:

Commit c276e4ae3fec434209c133c03d82849bcac99e57 ( HEAD -> master )
  • Author: divya < >
  • Date: Wed Dec 13 23:22:32 2017 -0800
  • Issue#901: Check for any missing tables in “production” database and report  
  • This script connects to the “production” database and produce a list of all “tables”.
  • Issue#901 referred to some of “production” tables missing after certain CRUD operation on Database.
  • A list of “prod” tables have been gathered; against which the present table names will be verified.
  • The script makes a connection to the “production” database reading the db_config file to get the username, password and database name.
  • The script then reads all the objects of type “table” and saves the table names in a file which is compared against the expected table names list.
  • If there is mis-match in both the files, the script generates a warning in the warning message file as well as sends out mail to the respective email id.

Usage: 

<script_name> <db_config> <db_name> <expectedTables_list_path>

Reported-by: whoever-reported-it
Signed-off-by: Your name <youremail@yourhost.com>

Single Commit Repo data

Diagram: Single commit repo data 

Git Object data for multiple commits 

Diagram: Git object data for multiple commits 

Stage changes as multiple commits: breaking commits into logical units by committing separate commits.

echo "bugfix#1234">>codes/mycode.sh
echo "Project: git workflow">>README.file

$git status
On branch master

No commits yet

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

modified:   README.file
modified:   codes/mycode.sh

git add code/code.sh
git commit -m "Add code for bug#1234"
git add README.file
git commit --m "Add project info"
  • View history and diffs
  • View project history (The ‘git log’ command)

Displays committed snapshots, you can filter or search for specific changes.

  • View history and diffs
  • View project history (The ‘git log’ command)

Displays committed snapshots, you can filter or search for specific changes.

3. View history and diffs

a)View project history (The ‘git log’ command)

Displays committed snapshots, you can filter or search for specific changes.

Project: proj_workFlow

commit 1e003594c6egit log
commit 1e003594c6e0f8850ec6d4fb9507f38e1ee80583 (HEAD -> master)
Author: divya bhushan <divya@developer.com>
Date:   Fri Nov 16 15:20:43 2018 +0530

    Add project info

commit d8f90e5e9949a43291f6206f522791c14bdda460
Author: divya bhushan <divya@developer.com>
Date:   Fri Nov 16 15:20:21 2018 +0530

   Add code for bug#1234

commit e6f82b5eb88863fb2d811ff5b2a1e18e76246bf4
Author: divya bhushan <divya@developer.com>
Date:   Fri Nov 16 15:17:01 2018 +0530

   Initial commit

Project: myProj

Log Example (1): git log

commit 3fbfa9a23bd7eac05226e08f40c0cb8138493619 (HEAD -> master)
Author: divya <divya@developer.com>
Date:   Wed Aug 22 16:53:26 2018 +0530

   Create the initial project structure

Log Example (2): Limiting the output to single line commit message

git log --oneline
3fbfa9a (HEAD -> master) 

Create the initial project structure

Log Example (3): Limit the log output to n number of commits

git log -n2
or
git log -2
git log -5 --oneline
9ecf5bd (HEAD -> master, myProj/master) Adding admin tasks.
fb5d4ee Merge branch 'master' of https://github.com/greets/myProj
34a4e65 Update README to version 3.3
43d4441 (tag: new, origin/master, origin/HEAD) Update README to version 3.2
0f491ba (origin/issue) Merge branch 'master' of https://github.com/greets/myProj

#To set aliases and create your own powerful and more resourceful commands:

1. Use alias “mylog” to display all logs in graphical one-liner format.

git config --global alias.mylog "log --graph --all --oneline"

2. Set the alias “hist” to print all the commits in pretty one-liner color coded format

git config --global alias.hist 'log --pretty=format:"%C(yellow)%h%Creset %ad | %C(green)%s%Creset%C(red)%d%Creset %C(blue)[%an]" --graph --decorate --date=short'
%h: abbreviated commit hash
%ad: author date
%s: subject
%d: ref names, like the --decorate options of git log
%an: author name

Log Example (4): Search for commits by a particular author

git hist --author="administrator"

OR

git log --author="administrator" --oneline
ce0341f added featureB
757ca44 modified temp.tmp file
c01d07a added featureB
3aec5b7 added temp info from admin user
13d556e added temp info
33b6e04 modified db.log file as admin user

Log Example (5): Include which files were altered and the relative number of lines that were added or deleted from each of them.

git log --stat 

git log --n 3 --stat
commit fb5d4eeb0a9be6a0e0c084ba51fd0308529c343e (HEAD -> master)
Merge: 34a4e65 43d4441
Author: divya <divya@developer.com>
Date:   Wed Aug 22 12:17:19 2018 +0530
   Merge branch 'master' of https://github.com/greets/myProj
commit 34a4e658489c99a2c42d1edf14c7c1d695df131a
Author: greets <developer2@github.com>
Date:   Mon Dec 18 04:53:04 2017 -0800
   Update README to version 3.3
README.md | 1 +
1 file changed, 1 insertion(+)
commit 43d4441bb60e4dc28ce07aaa2d4c37e8416eb061 (tag: new, origin/master, origin/HEAD)
Author: greets <developer2@github.com>
Date:   Mon Dec 18 04:53:04 2017 -0800
  
 Update README to version 3.2

README.md | 1 +
1 file changed, 1 insertion(+)

Log Example (6): Shows patch difference or full diff of each commit

git log --p
git log --n 2 --p
commit fb5d4eeb0a9be6a0e0c084ba51fd0308529c343e (HEAD -> master)
Merge: 34a4e65 43d4441
Author: divya <divya@developer.com>
Date:   Wed Aug 22 12:17:19 2018 +0530

  Merge branch 'master' of https://github.com/greets/myProj
commit 34a4e658489c99a2c42d1edf14c7c1d695df131a
Author: greets <developer2@github.com>
Date:   Mon Dec 18 04:53:04 2017 -0800
   Update README to version 3.3
diff --git a/README.md b/README.md
index c6f6cd4..14b4f8c 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,5 @@
# myProj
+#Version 3.3

This Project is created to get a good hands-on for the git development. You could edit this project locally working on your machine.

Log Example (7): Search for commits with a commit message that matches the grep pattern

git log --grep="hotfix"
git log --oneline --grep="iss53"
078f9f5 Merged iss53 into master after resolving conflict
ab3a5e5 Modified editInfo from iss53 branch
b777b16 modified editInfo from iss53 branch

Log Example (8): See if there’s any commits that have not been pushed to your origin remote.

#read this as: show the commits missing in origin, present in HEAD

git log origin..HEAD     
commit fb5d4eeb0a9be6a0e0c084ba51fd0308529c343e (HEAD -> master)
Merge: 34a4e65 43d4441
Author: divya <divya@developer.com>
Date:   Wed Aug 22 12:17:19 2018 +0530
   
   Merge branch 'master' of https://github.com/greets/myProj
commit 34a4e658489c99a2c42d1edf14c7c1d695df131a
Author: greets <developer2@github.com> 
Date:   Mon Dec 18 04:53:04 2017 -0800
    
    Update README to version 3.3

Log Example (9): Show commit logs missing in master, present in ‘topicBranch’  branch

git log --oneline master..topicBranch
commit 5d1e3baa9b790e3ce46cfc48499177c895ddbfb6 (topicBranch)
Author: divya <divya@developer.com>
Date:   Thu Sep 6 14:47:11 2018 +0530
   
  added runtest.t from featureB_old branch.

Log Example (10): commits that affected only a single file 

git log <file>
git log .gitignore
commit db263a6d1968d917c36688c34263980de591c040
Author: divya <divya@developer.com>
Date:   Sat Feb 28 12:38:28 2015 +0530
   pattern change in .gitignore file
commit fa676058abbfe61cb751d315a9942200e180020f
Author: divya <divya@developer.com>
Date:   Sat Feb 28 10:30:33 2015 +0530
   add the local .gitignore file

Log Example (11): logs after, before, after-before a period 

git log --after=”2015-02-27” -n2
git log --before=”2015-02-27”
git log --after="2017-12-01" --before="2018-07-01"

Log Example (12): Logs since a certain period

git log --since=”1 week ago”
git log --since=”yesterday”
git log --since=2.weeks -commit made in last 2 weeks
git log --since=1.day - commit made in last 1 day
git log --since=30.minutes - commit made in last 30 minutes
git log --since=5.hours - commit made in last 5 hours
git log --since=5.hours --pretty=oneline

Log Example (13): 38e1ee80583 (HEAD -> m

git log <since>..<until>
git log --since=4.months --until=2.weeks
git log --since="1 week ago" --until="yesterday"

Log Example (14): Display the logs between the commits with commit Ids

git log --oneline 8b92ffc.. 43d4441
43d4441 (tag: new, origin/master, origin/HEAD) Update README to version 3.2
0f491ba (origin/issue) Merge branch 'master' of https://github.com/greets/myProj
904ce2b Create README file

b) View changes between the 3 Tier of git(The ‘git diff’ command)

  • This command helps you to see the difference between Working directory, Staging area and Local repository.
  • There will be no output when the 2 tiers/layers have the same version of the code.

git diff between 3 layers of git workflow

Diagram: ‘git diff’ between 3 layers of git workflow

git diff
git diff --cached 
git diff HEAD

Project: proj_workFlow

Conclusion:

  • This command makes no change in either of the working dir, Index or local repo.
  • It only shows the differences between the 3 layers and how your code flows.
  • ‘git diff’ gives you an opportunity to review your code every time before you:

- edit your code in working directory

- add your code to the staging area(index)

- commit the code to local repository

4. Delete, rename and move files/directories

Git provide “git” commands to delete, rename and move files and directories.

git rm: Remove files from the working tree and from the index. Unlike ‘mv’ command ‘git rm’ will not remove a file from just your working directory.

git mv: Move or rename a file, a directory, or a symlink both in Working directory and Index.

cd proj_workFlow
git mv README.file README
git rm codes/mycode.sh
git mv temp/file.tmp .

git status

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

          renamed:    README.file -> README
          deleted:    codes/mycode.sh
          renamed:    temp/file.tmp -> file.tmp

5. Ignore file types

In certain cases you might need to ignore certain file types from being tracked and uploaded in the repository, such as compiled sources (.class, .dll..), Database log files(.sql, .log), packages(.7z, .gz, .jar, .tar etc), temporary OS generated files etc.

Git provides “.gitignore” file wherein you can mention the regex format for the filetypes you want git to ignore.

a) Create a local .gitignore file: vi .gitignore

  • Define a single “.gitignore” file in the root directory of the project.
  • Only meant for a specific project repository
  • This file has to be staged and committed every time there is an edit.
  • This file is shared among different users and repositories upon clone and pull/push.

b) Create a global .gitignore

Create a file under home directory of the user

vi ~/.gitignore_global

Define the file path under global config file.

git config --global core.excludesfile ~/.gitignore_global
  • The rules listed here will be applied to all the repositories on the local computer
  • This file will not be versioned and not distributed upon clone or pull/push with remote.

c) Repository personal ignore file on local computer

  • This file is not versioned and not distributed with your repository, i.e. not shared with other users.
vi .git/info/exclude

#Sample ignore patterns:

vi .gitignore
vi ~/.gitignore_global
vi .git/info/exclude
*.tmp -- ignore all files ending with tmp extension
*.[oa] -- all files ending with extension ‘o’ and ‘a’
*.exe -- ignore all files ending with exe
[tT]emp*.[oa] -- all files starting with temp or Temp and ending with an ‘o’ or ‘a’
[aA-zZ0-9]*
/test* -- Ignore filenames that start with a test - forward slash means beginning of path
temp/**/* -- Ignore all files under temp directory recursively
temp/**/*.tmp -- Ignore only *.tmp extension files under temp directory recursively
temp/*.tmp -- Ignore *.tmp files only under temp directory and not sub-directories
temp/* -- Ignore all files under dir temp

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

Ezeelogin

Thanks for the post. I liked the way all the details have been provided!

stephen

Firstly thanks for providing this tutorial from this article I have gathered lots of information. I can say that whatever the information provided by knowledgeHut is more useful to know more about the git.

Saurabh

Written nicely