Git: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
No edit summary
Line 65: Line 65:


''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''
<source lang="bash" highlight="8,9,12,13,21,22">
<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 88: Line 88:
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 152: Line 152:


==Git Amend Commit==
==Git Amend Commit==
<source lang="bash">
<syntaxhighlight lang="bash">
git commit --amend -C HEAD
git commit --amend -C HEAD
git push origin HEAD --force
git push origin HEAD --force
# git push origin <working-branch> --force
# git push origin <working-branch> --force
</source>
</syntaxhighlight>


==Git Merge Commits==
==Git Merge Commits==
Line 234: Line 234:
|
|
'''Local/Project Wise'''
'''Local/Project Wise'''
<source lang="bash">
<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
</source>
</syntaxhighlight>
|
|
'''Global/Workstation/User Wise'''
'''Global/Workstation/User Wise'''
<source lang="bash">
<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
</source>
</syntaxhighlight>
|
|
'''Strategy'''  
'''Strategy'''  
Line 258: Line 258:


==Git Https Remote Auth==
==Git Https Remote Auth==
<source lang="bash">
<syntaxhighlight lang="bash">
CODECOMMIT_USERNAME='<username>';\
CODECOMMIT_USERNAME='<username>';\
CODECOMMIT_PASSWORD='<password>';\
CODECOMMIT_PASSWORD='<password>';\
Line 266: Line 266:
password ${CODECOMMIT_PASSWORD}
password ${CODECOMMIT_PASSWORD}
EOF
EOF
</source>
</syntaxhighlight>


  git pull origin master --rebase
  git pull origin master --rebase
Line 273: Line 273:
{|
{|
|valign='top'|
|valign='top'|
<source lang="bash">
<syntaxhighlight lang="bash">
GIT_SSH_COMMAND="ssh\
GIT_SSH_COMMAND="ssh\
  -o StrictHostKeyChecking=no\
  -o StrictHostKeyChecking=no\
Line 281: Line 281:
/config.git $HOME/.config/chorke/academia/config_master 2>/dev/null\
/config.git $HOME/.config/chorke/academia/config_master 2>/dev/null\
  || git -C  $HOME/.config/chorke/academia/config_master pull
  || git -C  $HOME/.config/chorke/academia/config_master pull
</source>
</syntaxhighlight>


|valign='top'|
|valign='top'|
<source lang="bash">
<syntaxhighlight lang="bash">
export GIT_SSH_COMMAND="ssh\
export GIT_SSH_COMMAND="ssh\
  -o StrictHostKeyChecking=no\
  -o StrictHostKeyChecking=no\
Line 292: Line 292:
git clone --depth=1 --single-branch --branch master $GIT_CLONE_SOURCE\
git clone --depth=1 --single-branch --branch master $GIT_CLONE_SOURCE\
  $GIT_CLONE_TARGET 2>/dev/null || git -C $GIT_CLONE_TARGET pull
  $GIT_CLONE_TARGET 2>/dev/null || git -C $GIT_CLONE_TARGET pull
</source>
</syntaxhighlight>


|}
|}

Revision as of 20:58, 31 March 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 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 -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