Git provides configuration file to set the initial parameters for the repository. There are 3 levels of configurations:
Levels: | System | User | Project |
---|---|---|---|
Config file location: | /etc/gitconfig | ~/.gitconfig | Project/.git/config |
List the configurations | git config --system --list | git config --global --list | git config --list |
Edit the configuration: | git config --system --edit | git config --global --edit | git 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\] $"
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.
#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/
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)
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
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 )
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.sh
git add code/code.sh
git commit -m "Add code for bug#1234"
git add README.file
git commit --m "Add project info"
Displays committed snapshots, you can filter or search for specific changes.
Displays committed snapshots, you can filter or search for specific changes.
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
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
Diagram: ‘git diff’ between 3 layers of git workflow
git diff
git diff --cached
git diff HEAD
Project: proj_workFlow
- edit your code in working directory
- add your code to the staging area(index)
- commit the code to local repository
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
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
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
c) Repository personal ignore file on local computer
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
Thanks for the post. I liked the way all the details have been provided!
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.
Written nicely
Leave a Reply
Your email address will not be published. Required fields are marked *