Git: Difference between revisions
(55 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]" | ||
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''' | '''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 assume unchanged files''' | '''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 55: | Line 103: | ||
''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'' | ''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 | ||
Line 78: | 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 | ||
</ | </syntaxhighlight> | ||
==Git Branch== | ==Git Branch== | ||
Line 98: | Line 146: | ||
git push origin -d old-dev-branch;\ | git push origin -d old-dev-branch;\ | ||
git push origin new-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== | ==Git Commit== | ||
Line 108: | Line 175: | ||
'''delete commit from remote branch''' | '''delete commit from remote branch''' | ||
git push origin HEAD --force | 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''' | '''merge commit from another branch''' | ||
git cherry-pick <commit> | git cherry-pick <commit> | ||
==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 branch like '''staging''' or '''develop'''. During the time of your development let say you have added 50 | ==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"| | |valign="top"| | ||
'''Step: 1''' | '''Step: 1''' | ||
Line 142: | Line 231: | ||
'''Clean Dirty Branch & Start Again:''' | '''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"| | |valign="top"| | ||
Line 151: | Line 241: | ||
git branch -D '''<your_private_branch>''' | git branch -D '''<your_private_branch>''' | ||
git branch -d '''<your_private_branch>''' | git branch -d '''<your_private_branch>''' | ||
git checkout | 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== | ||
Line 160: | Line 273: | ||
| | | | ||
'''Local/Project Wise''' | '''Local/Project Wise''' | ||
< | <syntaxhighlight lang="bash"> | ||
git config pull.rebase false | git config pull.rebase false | ||
git config pull.rebase true | git config pull.rebase true | ||
git config pull.ff only | git config pull.ff only | ||
</ | </syntaxhighlight> | ||
| | | | ||
'''Global/Workstation/User Wise''' | '''Global/Workstation/User Wise''' | ||
< | <syntaxhighlight lang="bash"> | ||
git config --global pull.rebase false | git config --global pull.rebase false | ||
git config --global pull.rebase true | git config --global pull.rebase true | ||
git config --global pull.ff only | git config --global pull.ff only | ||
</ | </syntaxhighlight> | ||
| | | | ||
'''Strategy''' | '''Strategy''' | ||
Line 177: | Line 290: | ||
rebase (custom ) | rebase (custom ) | ||
fast-forward only | fast-forward only | ||
|} | |||
==Rename a Git Remote== | |||
git remote rename <old-name> <new-name> | |||
git remote rename '''origin''' '''gitea_origin''' | |||
==Git Https Remote Auth== | |||
<syntaxhighlight lang="bash"> | |||
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 | |||
</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 [email protected]: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='[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 | |||
</syntaxhighlight> | |||
|} | |} | ||
Line 186: | Line 342: | ||
git branch -d staging | git branch -d staging | ||
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 merge develop | ||
Line 204: | 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 249: | Line 413: | ||
| valign="top" | | | valign="top" | | ||
* [https://stackoverflow.com/questions/6372044/ Git merge a commit into another branch] | * [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/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://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" | | | 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" | | | 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 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