Reference Link
https://www.vogella.com/tutorials/Git/article.html
GIT is Distributed Version Control where as CVS is Central Code Repository
GIT will understand text files.
GIT will not be useful for non text files like
a. images,movies,music,fonts
b. word processing,spreadsheet,pdf
We need to install Git from https://git-scm.com/ in our system and install it
to look for the version of git
git version
To update the git use the below command
git update-git-for-windows
As git update is deprecated
To list command of git use the below command
git help
get help -g --> to view the guides
get help -a --> to get list of sub commands
to clone a GitHub repository use the below command
git clone <<url>>
To know the git configuration
git config --global --list
To initialize git in a directory use below
git init
http://git-scm.com --> To download Git
https://help.GitHub.com/articles/set-up-git
to verify Git version then use
git --version
System Level Configuration
c:\Program Files\Git\etc\gitconfig
c:\program data\Git\config
/etc/gitconfig (Unix)
To set the configuration at System level use the below
git config --System
User Level
c:\Users\<<username>>\.gitconfig
~/.gitconfig (Unix)
To set the configuration at User level use the below
git config --global
Project Level
<<project>>\.git\config
To set configuration at project level
git config
to view the configurations which was set the use the below command
git config --list
To remove a config property then use the below
git config --global --unset-all core.editor
To remove the section from config then use the below
git config --global --remove-section user
For just one repo:
git config user.name "Your Name Here"
git config user.email your@email.com
For (global) default email (which is configured in your ~/.gitconfig):
git config --global user.name "Your Name Here"
git config --global user.email your@email.com
Help command in Git
git help
git help log
git help init
To initialize the repository then we will use the below command in a directory where you project is present
git init
Once the above command is given the following hidden directory or folder will be created
.git
Inside .git you will see following directories /folder OR files
Folders
hooks
info
objects
ref
branches (will be created later)
logs ( Will be created later)
Files
description
config
HEAD
index ( Will be created later)
COMMIT_EDITMSG ( Will be created later)
We actually do the below basic steps in Git
a. Make Changes in the files
b. Add the changes to staging
c. Commit the changes to the repository with a message
Commit message best practices
a. Short single-line summary (less than 50 characters)
b. Optionally followed by a blank line and more complete description
c. Keep each line to less than 72 characters
d. Write commit message in present tense, not past tense
-"fix bug" or "fixes bug", not "fixed bug"
e. Bullet points are usually asterisks or hyphens
f. can add "Ticket number" from bugs or support requests
g. can develop shorthand for our organization
- "[css,js]"
- "bugfix:"
Git refers to commits (SHA-1)
Git refers to a change as a change set. So for a change set git generates a checksum for each change set.
To generate a checksum using SHA-1 hash algorithm. It is 40-character hexadecimal string
HEAD pointer in git moves to the current branch when we commit a change set.
To add new file(s) or modified file(s) into the repository then below command can be used
git add filename.txt
git add . --> To add/modify all the files use this command
To commit the changes the below command will be used
git commit -m "Initial commit"
To add and commit then use the below (new files and deletion of files the below command will not work properly and all modifications will be committed so make sure to use it)
git commit -ma <<Commit Message>>
To know the status between repository, staging and working directory and about the file changes
git status
To get the list of logs then use the below command
git log
To identify the changes between staging and working directory
git diff
git diff <<filename>>
git diff --color-words <<filename>>
To identify the changes between staging and Repository
git diff --staged
git diff --staged <<filename>>
git diff --staged --color-words <<filename>>
To delete the files we have two approach
Approach 1 :
a. Delete the file from Operating system and move the file to trash
b. git rm <<filename>>
c. git status
d. git commit -m "File deleted "
Approach 2:
a. git rm <<filename>>
b. git status
c. git commit -m "message"
git rm --cached <<filename>> --> Will delete from staging
To rename a file we have two approach
Approach 1 :
Approach 2 :
a. git mv <<filename>> <<new _filename>>
b. git status
c. git commit -m "message"
To replace a file in working directory from repository then use the following command
git checkout -- <<filename>> ( -- is making sure we are in current repository rather than
getting the branch name )
git checkout -- <<directory>>
To undo the changes from staging then use the below command
git reset HEAD <<filename>>
To ammend the changes to the last commit use the below command. Note ammend can be used for the latest head not to the previous head.
To revert the changes in the repository then use the below command
git revert <<SHA-1 key>>
To undo multiple commits
git reset
--soft
Does not change staging index or working directory
--mixed (default)
Changes staging index to match repository
Does not change working directory
--hard
Changes Staging index and working directory
git reset -- soft <<SHA key>>
git reset -- mixed <<SHA key>>
git reset -- hard<<SHA key>>
To remove all the untracked files and if the file is added to staging then it will not delete by below command
git clean -n --> shows which un-tracked files will be deleted
git clean -f --> deletes the file which are un-tracked
To ignore a particular file then we need to create file (.gitignore) in project directory
project/.gitignore --> The file contains all untracked files
very basic regular expressions
* ? [aeiou] [0-9]
negate expression with
*.php --> ignore all php files
!index.php --> don't ignore index.php file
ignore all files in a directory with trailing slash
assets/videos/
comment lines begin with #, blank lines are skipped in the .gitignore
https://help.github.com/articles/ignoring-files/
https://github.com/github/gitignore
To have the ignore at user level then use the below
git config --global core.exludesfile ~/.gitignore_global
Before using the above command create a file .gitignore_global
tree-ish
a. full SHA-1 hash
b. short SHA-1 hash
at least 4 characters
unambiguous(8-10 characters)
c. HEAD pointer
d. branch reference , tag reference
e. ancestry
parent commit
HEAD^,
HEAD~1
grandparents commit
git ls-tree HEAD
git ls-tree HEAD^
Different options in log
git log --since="2017-09-25"
git log --before="2017-09-25"
git log --since =" 2weeks ago" --until="3 weeks ago"
git log --author="pavan"
git log --grep="<<text>>"
git log --oneline
git log <<sha-1>> ..<<SHA-1>> --oneline
git log <SHA1>... <<filename>> --> To identify what changes have happened from the mentioned sha key
git log -p <<SHA1>>.. <<filename>> --> shows the difference also
git log --format=online --> this returns the full SHA key
git log --graph
git log --online --graph -all --decorate
To know what changes are done in change set then pass the SHA key
git show <<SHA1-key>>
git show --format=online HEAD~3
To compare two different commits
git diff <<SHA-1Key>>
git diff <<SHA-1Key>>..<<SHA-1Key>>
git diff <<SHA-1Key>>..<<SHA-1Key>> <<filename>>
git diff <<SHA-1Key>>..HEAD <<filename>>
git diff --stat --summary <<SHA-1Key>>..HEAD
git diff -w <<SHA-1Key>>..HEAD //To ignore the space changes
https://www.vogella.com/tutorials/Git/article.html
GIT is Distributed Version Control where as CVS is Central Code Repository
GIT will understand text files.
GIT will not be useful for non text files like
a. images,movies,music,fonts
b. word processing,spreadsheet,pdf
We need to install Git from https://git-scm.com/ in our system and install it
to look for the version of git
git version
To update the git use the below command
git update-git-for-windows
As git update is deprecated
To list command of git use the below command
git help
get help -g --> to view the guides
get help -a --> to get list of sub commands
to clone a GitHub repository use the below command
git clone <<url>>
To know the git configuration
git config --global --list
To initialize git in a directory use below
git init
http://git-scm.com --> To download Git
https://help.GitHub.com/articles/set-up-git
to verify Git version then use
git --version
System Level Configuration
c:\program data\Git\config
/etc/gitconfig (Unix)
To set the configuration at System level use the below
git config --System
User Level
c:\Users\<<username>>\.gitconfig
~/.gitconfig (Unix)
To set the configuration at User level use the below
git config --global
Project Level
<<project>>\.git\config
To set configuration at project level
git config
to view the configurations which was set the use the below command
git config --list
To remove a config property then use the below
git config --global --unset-all core.editor
To remove the section from config then use the below
git config --global --remove-section user
For just one repo:
git config user.name "Your Name Here"
git config user.email your@email.com
For (global) default email (which is configured in your ~/.gitconfig):
git config --global user.name "Your Name Here"
git config --global user.email your@email.com
Help command in Git
git help
git help log
git help init
man git-log or git help log are same
To initialize the repository then we will use the below command in a directory where you project is present
git init
Once the above command is given the following hidden directory or folder will be created
.git
Inside .git you will see following directories /folder OR files
Folders
hooks
info
objects
ref
branches (will be created later)
logs ( Will be created later)
Files
description
config
HEAD
index ( Will be created later)
COMMIT_EDITMSG ( Will be created later)
We actually do the below basic steps in Git
a. Make Changes in the files
b. Add the changes to staging
c. Commit the changes to the repository with a message
Commit message best practices
a. Short single-line summary (less than 50 characters)
b. Optionally followed by a blank line and more complete description
c. Keep each line to less than 72 characters
d. Write commit message in present tense, not past tense
-"fix bug" or "fixes bug", not "fixed bug"
e. Bullet points are usually asterisks or hyphens
f. can add "Ticket number" from bugs or support requests
g. can develop shorthand for our organization
- "[css,js]"
- "bugfix:"
Git refers to commits (SHA-1)
Git refers to a change as a change set. So for a change set git generates a checksum for each change set.
To generate a checksum using SHA-1 hash algorithm. It is 40-character hexadecimal string
HEAD pointer in git moves to the current branch when we commit a change set.
To add new file(s) or modified file(s) into the repository then below command can be used
git add filename.txt
git add . --> To add/modify all the files use this command
To commit the changes the below command will be used
git commit -m "Initial commit"
To add and commit then use the below (new files and deletion of files the below command will not work properly and all modifications will be committed so make sure to use it)
git commit -ma <<Commit Message>>
To know the status between repository, staging and working directory and about the file changes
git status
To get the list of logs then use the below command
git log
To identify the changes between staging and working directory
git diff
git diff <<filename>>
git diff --color-words <<filename>>
To identify the changes between staging and Repository
git diff --staged
git diff --staged <<filename>>
git diff --staged --color-words <<filename>>
To delete the files we have two approach
Approach 1 :
a. Delete the file from Operating system and move the file to trash
b. git rm <<filename>>
c. git status
d. git commit -m "File deleted "
Approach 2:
a. git rm <<filename>>
b. git status
c. git commit -m "message"
git rm --cached <<filename>> --> Will delete from staging
To rename a file we have two approach
Approach 1 :
Approach 2 :
a. git mv <<filename>> <<new _filename>>
b. git status
c. git commit -m "message"
To replace a file in working directory from repository then use the following command
git checkout -- <<filename>> ( -- is making sure we are in current repository rather than
getting the branch name )
git checkout -- <<directory>>
To undo the changes from staging then use the below command
git reset HEAD <<filename>>
To ammend the changes to the last commit use the below command. Note ammend can be used for the latest head not to the previous head.
To revert the changes in the repository then use the below command
git revert <<SHA-1 key>>
To undo multiple commits
git reset
--soft
Does not change staging index or working directory
--mixed (default)
Changes staging index to match repository
Does not change working directory
--hard
Changes Staging index and working directory
git reset -- soft <<SHA key>>
git reset -- mixed <<SHA key>>
git reset -- hard<<SHA key>>
To remove all the untracked files and if the file is added to staging then it will not delete by below command
git clean -n --> shows which un-tracked files will be deleted
git clean -f --> deletes the file which are un-tracked
To ignore a particular file then we need to create file (.gitignore) in project directory
project/.gitignore --> The file contains all untracked files
very basic regular expressions
* ? [aeiou] [0-9]
negate expression with
*.php --> ignore all php files
!index.php --> don't ignore index.php file
ignore all files in a directory with trailing slash
assets/videos/
comment lines begin with #, blank lines are skipped in the .gitignore
https://help.github.com/articles/ignoring-files/
https://github.com/github/gitignore
To have the ignore at user level then use the below
git config --global core.exludesfile ~/.gitignore_global
Before using the above command create a file .gitignore_global
tree-ish
a. full SHA-1 hash
b. short SHA-1 hash
at least 4 characters
unambiguous(8-10 characters)
c. HEAD pointer
d. branch reference , tag reference
e. ancestry
parent commit
HEAD^,
HEAD~1
grandparents commit
git ls-tree HEAD
git ls-tree HEAD^
Different options in log
git log --since="2017-09-25"
git log --before="2017-09-25"
git log --since =" 2weeks ago" --until="3 weeks ago"
git log --author="pavan"
git log --grep="<<text>>"
git log --oneline
git log <<sha-1>> ..<<SHA-1>> --oneline
git log <SHA1>... <<filename>> --> To identify what changes have happened from the mentioned sha key
git log -p <<SHA1>>.. <<filename>> --> shows the difference also
git log --format=online --> this returns the full SHA key
git log --graph
git log --online --graph -all --decorate
To know what changes are done in change set then pass the SHA key
git show <<SHA1-key>>
git show --format=online HEAD~3
To compare two different commits
git diff <<SHA-1Key>>
git diff <<SHA-1Key>>..<<SHA-1Key>>
git diff <<SHA-1Key>>..<<SHA-1Key>> <<filename>>
git diff <<SHA-1Key>>..HEAD <<filename>>
git diff --stat --summary <<SHA-1Key>>..HEAD
git diff -w <<SHA-1Key>>..HEAD //To ignore the space changes
Branches
In Git branches are cheap
- try new ideas
- isolate features or sections of work
One Working directory
Fast context switching
The below command will get the list of branches
git branch
git branch -a --> to know the local and remote branch
git branch -r --> To know the remote branch
To create a branch use the below command
git branch <<branch_name>>
To switch to branch use the below
git checkout <<branch_name>>
To checkout and create branch in single command
git checkout -b <<branch_name>>
While switching branches we need to make sure there are no uncommitted changes in the working directory if so then the following three option we have
a. checkout the previous file from the branch
b. Commit the changes to branch. (Even if you add the changes to staging it will not allow to swtich unless you commit to the branch)
c. stash the changes
Note: All tracking folder or files need to be clean while switching branches. If we have any new file or folder which or not tracked then it will not allow to switch between branches.
To compare two branches then we can use the below command
git diff <<branch_name>>..<<branch_name>>
To know the branch ancestor then use the below command in the branch
git branch --merge
To rename the branch
git branch --move <<branch_name>> <<new_branch_name>>
To delete the branch
git branch -d <<branch_name>>
git branch -D <<branch_name>> //When using -D be caution that it will delete even the changes are not merged into the master
To merge a branch A to B then we should be in B and then use the below command
git checkout A
git merge B
git merge --abort
git stash save <<Message for stash to remember>>
git stash list
git stash show stash@{0} (stash@{0} will be replaced with the exact stash number)
git stash apply <<stash number>>
git stash pop <<stash number>>
git stash drop <<stash number>>
git stash clear
git remote
Vendor who build repository on top of GIT are
a. Github
b. Gitlab
c. Bitbucket
d. Coding
https://medium.com/flow-ci/github-vs-bitbucket-vs-gitlab-vs-coding-7cf2b43888a1
Vendor who build repository on top of GIT are
a. Github
b. Gitlab
c. Bitbucket
d. Coding
https://medium.com/flow-ci/github-vs-bitbucket-vs-gitlab-vs-coding-7cf2b43888a1