10X Sale
kh logo
All Courses
  1. Tutorials
  2. DevOps

Working Locally With Git

Updated on Aug 29, 2025
 
13,125 Views

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:

  1. System-level configuration: applies to the entire computer that it is installed on
  2. User-level configuration: for a particular user
  3. 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:

Image

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.

Image

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

git init .

Image

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

Image

b) Add files/directories in the working directory

Image

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

Image
git status

Image

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

Image

git add README

Step 2: Check the status

Image

$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

Image

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

git status

Image

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

Image

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.

Image

git add .
git status

Image

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.
    Image
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 )
  1. Author: divya < >
  2. Date: Wed Dec 13 23:22:32 2017 -0800
  3. Issue#901: Check for any missing tables in “production” database and report
  4. This script connects to the “production” database and produce a list of all “tables”.
  5. Issue#901 referred to some of “production” tables missing after certain CRUD operation on Database.
  6. A list of “prod” tables have been gathered; against which the present table names will be verified.
  7. The script makes a connection to the “production” database reading the db_config file to get the username, password and database name.
  8. 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.
  9. 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>

Image

Image

Diagram: Single commit repo data

Image

Diagram: Git object data for multiple commits

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

Image

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

Image

$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

Image

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

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

  1. View history and diffs
  2. 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)

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

Image

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

Image

git diff
git diff --cached 
git diff HEAD

Project: proj_workFlow

Image

Conclusion:

  1. This command makes no change in either of the working dir, Index or local repo.
  2. It only shows the differences between the 3 layers and how your code flows.
  3. ‘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.

Image

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

git status

Image

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

  1. Define a single “.gitignore” file in the root directory of the project.
  2. Only meant for a specific project repository
  3. This file has to be staged and committed every time there is an edit.
  4. 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
  1. The rules listed here will be applied to all the repositories on the local computer
  2. 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
+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Get your free handbook for CSM!!
Recommended Courses