Git: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
 
(86 intermediate revisions by the same user not shown)
Line 2: Line 2:
  git config --global user.email "[email protected]"
  git config --global user.email "[email protected]"
   
   
  '''undo assume unchanged or assume unchanged'''  
git config --global pull.rebase true
git config --global core.autocrlf input
git config --global init.defaultBranch develop
git config advice.skippedCherryPicks false
 
  '''undo or remove from index'''
git rm --cache src/test/resources/application.properties
 
'''undo or redo assume unchanged'''  
  git update-index --no-assume-unchanged src/main/resources/application.properties
  git update-index --no-assume-unchanged src/main/resources/application.properties
  git update-index --assume-unchanged src/main/resources/application.properties
  git update-index --assume-unchanged src/main/resources/application.properties
 
  '''list of files assume unchanged'''
  '''list of assume unchanged files'''
  git ls-files -v|grep '^h'
  git ls-files -v|grep '^h'


Line 25: Line 33:
  '''clear metadata'''
  '''clear metadata'''
  rm -rf */out */*.iml */*.ipr */*.iws */.idea */cmake-build-* */.idea_modules
  rm -rf */out */*.iml */*.ipr */*.iws */.idea */cmake-build-* */.idea_modules
==Git Alias==
{|
| valign="top" |
git config --global alias.lo "log --oneline"
git log --oneline
git lo
| valign="top" |
git config --global --unset-all alias.lo
git config --global --unset    alias.lo
git lo
|-
| colspan="2" |
----
|-
| valign="top" |
git config --global alias.lp 'log --pretty="%h %cD %cn %s"'
git lp
| valign="top" |
git config --global --unset-all alias.lp
git lp
|-
| colspan="2" |
----
|-
| valign="top" |
git config --global alias.dtag "describe --tags $(git rev-list --tags --max-count=1)"
git dtag
| valign="top" |
git config --global --unset-all alias.dtag
git dtag
|}


==Git Stash==
==Git Stash==
Line 35: Line 81:
  git stash clear
  git stash clear
  git stash list
  git stash list
git stash


| valign="top" |
| valign="top" |
Line 43: Line 90:
  git stash apply stash^{/stash_v1.0.00}
  git stash apply stash^{/stash_v1.0.00}
  git stash push -m stash_v1.0.00
  git stash push -m stash_v1.0.00
git stash drop
|}
|}


Line 53: Line 101:
  git tag    v1.5_initial_setup
  git tag    v1.5_initial_setup


<source lang="bash" highlight="7,8">
 
''By default git sort order is ascending, for descending order we need to add <code>-</code> sign before the value of <code>--sort</code> parameter''
<syntaxhighlight lang="bash" highlight="8,9,12,13,21,22">
git tag    --sort=taggerdate
git tag    --sort=taggerdate
git tag -l --sort=taggerdate
git tag -l --sort=taggerdate
   
   
git tag -l --format='%(taggerdate) %(refname)'
git tag -l --format='%(taggerdate) %(refname)'
git tag -l --format='%(taggerdate) %(refname)' --sort=taggerdate
git tag -l --format='%(taggerdate) %(refname)'   --sort=taggerdate
 
# ascending order
git tag -l --format='%(taggerdate)%09%(refname)'  --sort=taggerdate
git tag -l --format='%(taggerdate)%09%(refname)'  --sort=taggerdate
git tag -l --format='%(creatordate)%09%(refname)' --sort=creatordate
git tag -l --format='%(creatordate)%09%(refname)' --sort=creatordate
# descending order
git tag -l --format='%(taggerdate)%09%(refname)'  --sort=-taggerdate
git tag -l --format='%(creatordate)%09%(refname)' --sort=-creatordate


git tag --format='%(creatordate:short)%09%(refname:strip=2)'
git tag --format='%(creatordate:short)%09%(refname:strip=2)'
Line 71: Line 126:
git log --tags --simplify-by-decoration --pretty='format:%ai %d'
git log --tags --simplify-by-decoration --pretty='format:%ai %d'
git log --tags --simplify-by-decoration --pretty='format:%ai %h %d' --date-order --graph
git log --tags --simplify-by-decoration --pretty='format:%ai %h %d' --date-order --graph
</source>
</syntaxhighlight>


==Git Branch==
==Git Branch==
Line 85: Line 140:
  git push  -d origin branch_to_delete
  git push  -d origin branch_to_delete
  git push origin --delete branch_to_delete
  git push origin --delete branch_to_delete
'''renaming branch name'''
git checkout develop;\
git checkout -b new-dev-branch old-dev-branch;\
git push origin -d old-dev-branch;\
git push origin new-dev-branch
==Git Rebase==
git checkout '''<user-branch>'''
git pull --rebase origin '''<source-branch>'''
''user branches '''merge into''' official branch''
─────────────────────────────────────────
'''merge :''' user-branch '''»''' develop|master
'''merge :''' user-branch '''»''' '''staging|uat|production'''
''official branches '''rebase into''' user branch''
──────────────────────────────────────────
'''rebase:''' user-branch '''«''' develop|master
'''rebase:''' user-branch '''«''' '''staging|uat|production'''
''official branches '''merge into''' official branch''
─────────────────────────────────────────────
'''merge :''' develop '''»''' uat '''»''' production '''»''' master
'''merge :''' staging '''»''' uat '''»''' production '''»''' master
==Git Commit==
'''delete latest commit'''
git reset --hard HEAD~1
'''rollback to the commit id'''
git reset --hard <sha1-commit-id>
'''delete commit from remote branch'''
git push origin HEAD --force
'''delete commit from local branch'''
git checkout <your-branch>
git fetch --all && git reset --hard origin/<your-branch>
'''merge commit from another branch'''
git cherry-pick <commit>
==Git Reflog==
  '''restore commit'''
  git reflog
  git reset --hard <sha value>
==Git Amend Commit==
<syntaxhighlight lang="bash">
git commit --amend
git commit --amend -C HEAD
git push origin HEAD --force
# git push origin <working-branch> --force
</syntaxhighlight>
==Git Merge Commits==
Let say there is a new feature needed to develop yourself. During the time of feature creation you have created <code>git checkout -b your_private_branch</code> branch from any others branch like '''staging''' or '''develop'''. During the time of your development, let say you have added 50 commits for your own tracking. Now you needs to delete 49 commits and have to append your commit with the first commit. After that you need to update your central repository.
{|
|colspan="2" valign="top"|
'''Merge Commits Into Single:'''
----
|-
|valign="top"|
'''Step: 1'''
git checkout '''<your_private_branch>'''
git status
git log
'''Step: 2'''
git reset --soft '''<hash_of_first_commit>'''
git status
git log
|valign="top"|
'''Step: 3'''
git commit --amend -m ''''<comment_about_feature>''''
git status
git log
'''Step: 4 (final)'''
git push origin HEAD --force
git status
git log
|-
|colspan="2" valign="top"|
'''Clean Dirty Branch & Start Again:'''
----
For safety purpose you should check status and log to ensure your are not doing any wrong. Always be careful before <code> git push origin HEAD --force</code>. Before <code> git push origin HEAD --force</code> please keep backup of your branch into any other locations to avoid any kind of disaster & fast recovery <code>git pull origin your_private_branch</code>.
|-
|valign="top"|
git stash
git stash clear
git checkout '''<any_other_branch>'''
|valign="top"|
git branch -D  '''<your_private_branch>'''
git branch -d  '''<your_private_branch>'''
git checkout  '''<your_private_branch>'''
|}
==Git Copy Changes==
'''diff changes:'''
git diff > ~/Documents/ThinkPad_L14Gen3/my-diff-changes.txt
scp legion:~/Documents/ThinkPad_L14Gen3/my-diff-changes.txt ~/Documents/ThinkPad_L14Gen3/
git apply  ~/Documents/ThinkPad_L14Gen3/my-diff-changes.txt
'''tracked changes only:'''
git stash save my-tracked-changes
git stash show -p > ~/Documents/ThinkPad_L14Gen3/my-tracked-changes.txt
git stash pop
scp legion:~/Documents/ThinkPad_L14Gen3/my-tracked-changes.txt ~/Documents/ThinkPad_L14Gen3/
git apply  ~/Documents/ThinkPad_L14Gen3/my-tracked-changes.txt
'''include untracked changes:'''
git stash save --include-untracked my-untracked-changes
git stash show -p > ~/Documents/ThinkPad_L14Gen3/my-untracked-changes.txt
git stash pop
scp legion:~/Documents/ThinkPad_L14Gen3/my-untracked-changes.txt ~/Documents/ThinkPad_L14Gen3/
git apply  ~/Documents/ThinkPad_L14Gen3/my-untracked-changes.txt


==Git Warning==
==Git Warning==
* ''Pulling without specifying how to reconcile divergent branches is discouraged. You can squelch this message by running one of the following commands sometime before your next pull:''
''Pulling without specifying how to reconcile divergent branches is discouraged. You can squelch this message by running one of the following commands sometime before your next pull:''
{|
|
'''Local/Project Wise'''
<syntaxhighlight lang="bash">
git config pull.rebase false
git config pull.rebase true
git config pull.ff only
</syntaxhighlight>
|
'''Global/Workstation/User Wise'''
<syntaxhighlight lang="bash">
git config --global pull.rebase false
git config --global pull.rebase true
git config --global pull.ff only
</syntaxhighlight>
|
'''Strategy'''
merge  (default)
rebase  (custom )
fast-forward only
|}
 
==Rename a Git Remote==
git remote rename <old-name> <new-name>
git remote rename '''origin''' '''gitea_origin'''


<source lang="bash">
==Git Https Remote Auth==
git config pull.rebase false # merge (the default strategy)
<syntaxhighlight lang="bash">
git config pull.rebase true  # rebase
CODECOMMIT_USERNAME='<username>';\
git config pull.ff only      # fast-forward only
CODECOMMIT_PASSWORD='<password>';\
</source>
tee ${HOME}/.netrc >/dev/null <<EOF
machine git-codecommit.ap-southeast-1.amazonaws.com
login ${CODECOMMIT_USERNAME}
password ${CODECOMMIT_PASSWORD}
EOF
</syntaxhighlight>
 
git pull origin master --rebase
 
==Git SSH Command==
{|
|valign='top'|
<syntaxhighlight lang="bash">
GIT_SSH_COMMAND="ssh\
-o StrictHostKeyChecking=no\
  -o UserKnownHostsFile=/dev/null"\
git clone --depth=1 --single-branch\
--branch master git@scm.chorke.org:chorke/academia\
/config.git $HOME/.config/chorke/academia/config_master 2>/dev/null\
|| git -C  $HOME/.config/chorke/academia/config_master pull
</syntaxhighlight>
 
|valign='top'|
<syntaxhighlight lang="bash">
export GIT_SSH_COMMAND="ssh\
-o StrictHostKeyChecking=no\
-o UserKnownHostsFile=/dev/null";\
export GIT_CLONE_TARGET="${HOME}/.config/chorke/academia/config_master";\
export GIT_CLONE_SOURCE='git@scm.chorke.org:chorke/academia/config.git';\
git clone --depth=1 --single-branch --branch master $GIT_CLONE_SOURCE\
$GIT_CLONE_TARGET 2>/dev/null || git -C $GIT_CLONE_TARGET pull
</syntaxhighlight>
 
|}


==Knowledge==
==Knowledge==
Line 102: Line 342:
  git branch -d staging
  git branch -d staging
  git branch -D staging
  git branch -D staging
'''debug ssh connection'''


  git merge develop
  git merge develop
  git fetch origin staging
  git fetch origin staging
  git checkout -b staging origin/staging
  git checkout -b staging origin/staging
   
 
  git config --global init.defaultBranch master
 
  '''git stashing'''
  '''git stashing'''
  git stash; git checkout -b new_branch; git stash pop
  git stash; git checkout -b new_branch; git stash pop
Line 118: Line 365:
  git merge --squash feature #all the feature commits replaced by a new commit in the master
  git merge --squash feature #all the feature commits replaced by a new commit in the master
  git commit -m 'feature merged with master branch'
  git commit -m 'feature merged with master branch'
git checkout uat
git pull --rebase origin staging


==References==
==References==
Line 146: Line 396:


| valign="top" |
| valign="top" |
* [https://www.digitalocean.com/community/tutorials/how-to-use-git-hooks-to-automate-development-and-deployment-tasks Git Hooks To Automate Development and Deployment]
* [https://stackoverflow.com/questions/28166547/ Differences of <code>revert</code>, <code>amend</code>, <code>rollback</code> & <code>undo</code>]
* [https://gist.github.com/noelboss/3fe13927025b89757f8fb12e9066f2fa Automated Git Deployment using Git Hooks]
* [https://medium.com/@rishpandey/simplest-auto-deployment-using-git-hooks-4cd6d98e0fc6 Simplest auto-deployment using git hooks]
* [https://stackoverflow.com/questions/1338728/ Git Delete Commit from Local and Remote]
* [https://stackoverflow.com/questions/18276951/ Git stop tracking any changes of a file]
* [https://stackoverflow.com/questions/2003505/ Git Delete Local and Remote Branch]
* [https://stackoverflow.com/questions/2003505/ Git Delete Local and Remote Branch]
* [https://stackoverflow.com/questions/14998923/ Create a new Branch from a Branch]
* [https://stackoverflow.com/questions/14998923/ Create a new Branch from a Branch]
* [https://stackoverflow.com/questions/6900328/ Git show all (lightweight) Tags]
* [https://stackoverflow.com/questions/6900328/ Git show all (lightweight) Tags]
* [https://git-scm.com/book/en/v2/Distributed-Git-Maintaining-a-Project Git Maintaining a Project]
|-
| colspan="3" |
----
|-
| valign="top" |
* [https://stackoverflow.com/questions/6372044/ Git merge a commit into another branch]
* [https://stackoverflow.com/questions/17577409/ Git remove merge commit from history]
* [https://stackoverflow.com/questions/31854476/ Git Move Local Changes From PC to PC]
* [https://stackoverflow.com/questions/1670970/ Cherry-pick multiple commits]
* [https://stackoverflow.com/questions/4526910/ Git Rename a Submodule]
* [https://stackoverflow.com/questions/33840617/ Rename a git remote]
* [https://www.syntevo.com/smartgit/download/ Download SmartGit]
* [https://stackoverflow.com/questions/21414725/ Git tags sort order]
* [https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase Git Rebase]
* [https://www.atlassian.com/git/tutorials/gitk Gitk]
| valign="top" |
* [https://stackoverflow.com/questions/1139762/ Ignore files that have already been committed]
* [https://docs.github.com/en/pull-requests/committing-changes-to-your-project/creating-and-editing-commits/changing-a-commit-message Git » Changing a commit message]
* [https://stackoverflow.com/questions/7772190/ Passing ssh options to git clone]
* [[Base64]]
| valign="top" |


|}
|}

Latest revision as of 02:40, 2 April 2024

git config --global user.name "Full Name"
git config --global user.email "[email protected]"

git config --global pull.rebase true
git config --global core.autocrlf input
git config --global init.defaultBranch develop
git config advice.skippedCherryPicks false
undo or remove from index 
git rm --cache src/test/resources/application.properties
undo or redo assume unchanged 
git update-index --no-assume-unchanged src/main/resources/application.properties
git update-index --assume-unchanged src/main/resources/application.properties
list of assume unchanged files
git ls-files -v|grep '^h'

Git Ignore

out/
*.iml
*.ipr
*.iws
.idea/*
cmake-build-*/
.idea_modules/
fabric.properties
crashlytics.properties
atlassian-ide-plugin.xml
crashlytics-build.properties
com_crashlytics_export_strings.xml
clear metadata
rm -rf */out */*.iml */*.ipr */*.iws */.idea */cmake-build-* */.idea_modules

Git Alias

git config --global alias.lo "log --oneline"
git log --oneline
git lo
git config --global --unset-all alias.lo
git config --global --unset     alias.lo
git lo

git config --global alias.lp 'log --pretty="%h %cD %cn %s"'
git lp
git config --global --unset-all alias.lp
git lp

git config --global alias.dtag "describe --tags $(git rev-list --tags --max-count=1)"
git dtag
git config --global --unset-all alias.dtag
git dtag

Git Stash

git stash apply stash@{index}
git stash drop  stash@{index}
git stash pop   stash@{index}

git stash clear
git stash list
git stash
git apply --3way ~/.chorke/tmp/academia/patch/v1.0.00.patch
git apply  ~/.chorke/tmp/academia/patch/v1.0.00.patch
git diff > ~/.chorke/tmp/academia/patch/v1.0.00.patch

git stash apply stash^{/stash_v1.0.00}
git stash push -m stash_v1.0.00
git stash drop

Git Tag

annotated tag
git tag -a v1.5_initial_setup -m 'initial setup'
git tag -a v1.5_initial_setup
lightweight tag
git tag    v1.5_initial_setup


By default git sort order is ascending, for descending order we need to add - sign before the value of --sort parameter

git tag    --sort=taggerdate
git tag -l --sort=taggerdate
 
git tag -l --format='%(taggerdate) %(refname)'
git tag -l --format='%(taggerdate) %(refname)'    --sort=taggerdate

# ascending order
git tag -l --format='%(taggerdate)%09%(refname)'  --sort=taggerdate
git tag -l --format='%(creatordate)%09%(refname)' --sort=creatordate

# descending order
git tag -l --format='%(taggerdate)%09%(refname)'  --sort=-taggerdate
git tag -l --format='%(creatordate)%09%(refname)' --sort=-creatordate

git tag --format='%(creatordate:short)%09%(refname:strip=2)'
git tag --format='%(creatordate:short)%09%(refname:strip=2)' --sort=creatordate
 
git tag --format='%(taggerdate:short)%09%(refname:strip=2)'
git tag --format='%(taggerdate:short)%09%(refname:strip=2)'  --sort=taggerdate

git log --tags --simplify-by-decoration --pretty='format:%ai %d'
git log --tags --simplify-by-decoration --pretty='format:%ai %h %d' --date-order --graph

Git Branch

create and checkout a new branch
git checkout -b new_branch_name base_branch_name
git checkout -b new_branch_name
renaming and delete branch
git branch -m old_branch_name new_branch_name
git branch -D branch_to_delete_without_merge_status

git branch -d branch_to_delete
git push   -d origin branch_to_delete
git push origin --delete branch_to_delete
renaming branch name
git checkout develop;\
git checkout -b new-dev-branch old-dev-branch;\
git push origin -d old-dev-branch;\
git push origin new-dev-branch

Git Rebase

git checkout <user-branch>
git pull --rebase origin <source-branch>
user branches merge into official branch
─────────────────────────────────────────
merge : user-branch » develop|master
merge : user-branch » staging|uat|production
official branches rebase into user branch
──────────────────────────────────────────
rebase: user-branch « develop|master
rebase: user-branch « staging|uat|production
official branches merge into official branch
─────────────────────────────────────────────
merge : develop » uat » production » master
merge : staging » uat » production » master

Git Commit

delete latest commit
git reset --hard HEAD~1

rollback to the commit id
git reset --hard <sha1-commit-id>

delete commit from remote branch
git push origin HEAD --force
delete commit from local branch
git checkout <your-branch>
git fetch --all && git reset --hard origin/<your-branch>
merge commit from another branch
git cherry-pick <commit>


Git Reflog

 restore commit
 git reflog
 git reset --hard <sha value>

Git Amend Commit

git commit --amend
git commit --amend -C HEAD
git push origin HEAD --force
# git push origin <working-branch> --force

Git Merge Commits

Let say there is a new feature needed to develop yourself. During the time of feature creation you have created git checkout -b your_private_branch branch from any others branch like staging or develop. During the time of your development, let say you have added 50 commits for your own tracking. Now you needs to delete 49 commits and have to append your commit with the first commit. After that you need to update your central repository.

Merge Commits Into Single:


Step: 1
git checkout <your_private_branch>
git status
git log
Step: 2
git reset --soft <hash_of_first_commit>
git status
git log
Step: 3
git commit --amend -m '<comment_about_feature>'
git status
git log
Step: 4 (final)
git push origin HEAD --force
git status
git log 

Clean Dirty Branch & Start Again:


For safety purpose you should check status and log to ensure your are not doing any wrong. Always be careful before git push origin HEAD --force. Before git push origin HEAD --force please keep backup of your branch into any other locations to avoid any kind of disaster & fast recovery git pull origin your_private_branch.

git stash
git stash clear
git checkout <any_other_branch>
git branch -D  <your_private_branch>
git branch -d  <your_private_branch>
git checkout   <your_private_branch>

Git Copy Changes

diff changes:
git diff > ~/Documents/ThinkPad_L14Gen3/my-diff-changes.txt

scp legion:~/Documents/ThinkPad_L14Gen3/my-diff-changes.txt ~/Documents/ThinkPad_L14Gen3/
git apply  ~/Documents/ThinkPad_L14Gen3/my-diff-changes.txt
tracked changes only:
git stash save my-tracked-changes
git stash show -p > ~/Documents/ThinkPad_L14Gen3/my-tracked-changes.txt
git stash pop

scp legion:~/Documents/ThinkPad_L14Gen3/my-tracked-changes.txt ~/Documents/ThinkPad_L14Gen3/
git apply  ~/Documents/ThinkPad_L14Gen3/my-tracked-changes.txt
include untracked changes:
git stash save --include-untracked my-untracked-changes
git stash show -p > ~/Documents/ThinkPad_L14Gen3/my-untracked-changes.txt
git stash pop

scp legion:~/Documents/ThinkPad_L14Gen3/my-untracked-changes.txt ~/Documents/ThinkPad_L14Gen3/
git apply  ~/Documents/ThinkPad_L14Gen3/my-untracked-changes.txt

Git Warning

Pulling without specifying how to reconcile divergent branches is discouraged. You can squelch this message by running one of the following commands sometime before your next pull:

Local/Project Wise

git config pull.rebase false
git config pull.rebase true
git config pull.ff only

Global/Workstation/User Wise

git config --global pull.rebase false
git config --global pull.rebase true
git config --global pull.ff only

Strategy

merge   (default)
rebase  (custom )
fast-forward only

Rename a Git Remote

git remote rename <old-name> <new-name>
git remote rename origin gitea_origin

Git Https Remote Auth

CODECOMMIT_USERNAME='<username>';\
CODECOMMIT_PASSWORD='<password>';\
tee ${HOME}/.netrc >/dev/null <<EOF
machine git-codecommit.ap-southeast-1.amazonaws.com
login ${CODECOMMIT_USERNAME}
password ${CODECOMMIT_PASSWORD}
EOF
git pull origin master --rebase

Git SSH Command

GIT_SSH_COMMAND="ssh\
 -o StrictHostKeyChecking=no\
 -o UserKnownHostsFile=/dev/null"\
 git clone --depth=1 --single-branch\
 --branch master [email protected]:chorke/academia\
/config.git $HOME/.config/chorke/academia/config_master 2>/dev/null\
 || git -C  $HOME/.config/chorke/academia/config_master pull
export GIT_SSH_COMMAND="ssh\
 -o StrictHostKeyChecking=no\
 -o UserKnownHostsFile=/dev/null";\
export GIT_CLONE_TARGET="${HOME}/.config/chorke/academia/config_master";\
export GIT_CLONE_SOURCE='[email protected]:chorke/academia/config.git';\
git clone --depth=1 --single-branch --branch master $GIT_CLONE_SOURCE\
 $GIT_CLONE_TARGET 2>/dev/null || git -C $GIT_CLONE_TARGET pull

Knowledge

discard changes
git checkout master -f
delete and recreate
git branch -d staging
git branch -D staging
debug ssh connection
ssh -v  [email protected]
ssh -T  [email protected]
ssh -vT [email protected]
git merge develop
git fetch origin staging
git checkout -b staging origin/staging
git config --global init.defaultBranch master
git stashing
git stash; git checkout -b new_branch; git stash pop
git add .; git commit -m "new feature added"; git push
git remote -v;mv src/main/resources/application.properties ..;cd ..
mv ../application.properties src/main/resources/

rebase vs merge
git rebase master #all the updated commits of master will be available in the feature branch
git merge --squash feature #all the feature commits replaced by a new commit in the master
git commit -m 'feature merged with master branch'

git checkout uat
git pull --rebase origin staging

References