Difference between revisions of "IT-SDK-Git"
Jump to navigation
Jump to search
(→.bashrc & .zshrc) |
Samerhijazi (talk | contribs) (→Configuration) |
||
| (62 intermediate revisions by 2 users not shown) | |||
| Line 16: | Line 16: | ||
=Configuration= | =Configuration= | ||
| + | * '''Layers''': | ||
| + | ** Remote-Repo | ||
| + | ** Local-Repo | ||
| + | ** Staging-Area (Index) | ||
| + | ** Working-Area (Directory) | ||
* '''Stages''': worktree, staged(index), HEAD | * '''Stages''': worktree, staged(index), HEAD | ||
| − | =.bashrc | + | * HEAD refers to the latest commit in your current '''branch'''. |
| + | |||
| + | =Shell & Git= | ||
| + | ==Bash (.bashrc)== | ||
<pre class="code"> | <pre class="code"> | ||
git_branch() { | git_branch() { | ||
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/' | git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/' | ||
} | } | ||
| + | PS1='\[\033[32m\][\w]\[\033[31m\] $(git_branch)\[\033[0m\]\n$ ' | ||
| + | </pre> | ||
| + | ==Zsh (.zshrc)== | ||
| + | <pre class="code"> | ||
| + | git_branch() { | ||
| + | git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/' | ||
| + | } | ||
| + | setopt PROMPT_SUBST | ||
| + | PROMPT='[%F{green}%d%f]%F{red} $(git_branch)%f | ||
| + | $ ' | ||
| + | </pre> | ||
| + | ==Alternativ== | ||
| + | <pre class="code"> | ||
git_branch() { | git_branch() { | ||
branch=$(git symbolic-ref --short HEAD 2> /dev/null) | branch=$(git symbolic-ref --short HEAD 2> /dev/null) | ||
| Line 30: | Line 51: | ||
fi | fi | ||
} | } | ||
| − | |||
| − | |||
| − | |||
| − | |||
</pre> | </pre> | ||
| − | ==Global== | + | ==Global-Settings== |
* https://www.git-scm.com/book/en/v2/Appendix-C%3A-Git-Commands-Setup-and-Config | * https://www.git-scm.com/book/en/v2/Appendix-C%3A-Git-Commands-Setup-and-Config | ||
| + | * https://www.scootersoftware.com/kb/vcs | ||
<pre class="code"> | <pre class="code"> | ||
git config --local --list # List Configs for repos. config file "repo/.git/config" | git config --local --list # List Configs for repos. config file "repo/.git/config" | ||
git config --global --list # List Configs for global config file "~/.gitconfig" | git config --global --list # List Configs for global config file "~/.gitconfig" | ||
git config --system --list # List Configs for System config file "installed/git/etc/gitconfig" | git config --system --list # List Configs for System config file "installed/git/etc/gitconfig" | ||
| − | --- | + | ----------------------------------------------------------------------------------------------------- |
| − | git config --global --edit | + | git config --global --edit ### Edit Configs |
| − | git config --global user.name | + | git config --global user.name ### Show config for user.name |
| − | git config --global user.name "King" | + | git config --global user.name "King" ### Set Value "King" to user.name |
| − | git config --global --unset user.name | + | git config --global --unset user.name ### Unset saved user.name |
| + | git config --global --unset user.password ### Unset saved user.password | ||
</pre> | </pre> | ||
<pre class="code"> | <pre class="code"> | ||
git config --global user.name "Samer Hijazi" | git config --global user.name "Samer Hijazi" | ||
| − | git config --global user.email " | + | git config --global user.email "samerhijazi@samerhijazi.net" |
| − | ---- | + | ----------------------------------------------------------------------------------------------------- |
git config --global alias.ss 'status -s' | git config --global alias.ss 'status -s' | ||
| + | git config --global alias.sm 'diff --name-only --diff-filter=M' | ||
| + | git config --global alias.sd 'diff --name-only --diff-filter=D' | ||
git config --global alias.rs 'reset --hard origin/master' | git config --global alias.rs 'reset --hard origin/master' | ||
| + | git config --global alias.cm 'commit -m' | ||
git config --global alias.cp '!git commit -a -m "Update $1" && git push' | git config --global alias.cp '!git commit -a -m "Update $1" && git push' | ||
| − | ---- | + | git config --global alias.xx '!git fetch origin && git reset --hard origin/master && git clean -fdx' |
| + | git config --global alias.xx '!git checkout -- . && git reset --hard HEAD && git clean -fdx' | ||
| + | ----------------------------------------------------------------------------------------------------- | ||
| + | git config --global core.editor "code --wait" | ||
git config --global core.editor "nano" | git config --global core.editor "nano" | ||
git config --global core.editor "vim" | git config --global core.editor "vim" | ||
git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin" | git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin" | ||
| − | ---- | + | ----------------------------------------------------------------------------------------------------- |
git config --global http.proxy $PROXY_URL | git config --global http.proxy $PROXY_URL | ||
git config --global https.proxy $PROXY_URL | git config --global https.proxy $PROXY_URL | ||
| − | ---- | + | git config --global http.https://devstack.samerhijazi.com/.extraHeader "Authorization: Bearer XXX" |
| + | ----------------------------------------------------------------------------------------------------- | ||
git config --global core.autocrlf true # Working on Windows, Linux, macOS Maschines. | git config --global core.autocrlf true # Working on Windows, Linux, macOS Maschines. | ||
git config --global core.safecrlf false # Disable showing Warnning for LF | git config --global core.safecrlf false # Disable showing Warnning for LF | ||
git config --global core.longpaths true | git config --global core.longpaths true | ||
| − | ---- | + | git config --global core.compression 9 # compression for speed up push process. |
| + | ----------------------------------------------------------------------------------------------------- | ||
git config --global diff.tool bc | git config --global diff.tool bc | ||
git config --global difftool.bc.path "C:/Program Files/Beyond Compare 4/bcomp.exe" | git config --global difftool.bc.path "C:/Program Files/Beyond Compare 4/bcomp.exe" | ||
| − | + | ----------------------------------------------------------------------------------------------------- | |
git config --global merge.tool bc | git config --global merge.tool bc | ||
git config --global mergetool.bc.path "C:/Program Files/Beyond Compare 4/bcomp.exe" | git config --global mergetool.bc.path "C:/Program Files/Beyond Compare 4/bcomp.exe" | ||
| − | ---- | + | ----------------------------------------------------------------------------------------------------- |
| − | git config --global advice.addIgnoredFile false # Turen off advice for adding Ignored files | + | git config --global advice.addIgnoredFile false ### Turen off advice for adding Ignored files |
| − | ---- | + | git config --global pull.rebase true ### Local changes will rebased on top of the changes pulled from the remote branch. |
| + | ----------------------------------------------------------------------------------------------------- | ||
git commit --amend --reset-author | git commit --amend --reset-author | ||
| + | |||
</pre> | </pre> | ||
| + | ===extras=== | ||
<pre class="code"> | <pre class="code"> | ||
# https://mattferderer.com/fix-git-self-signed-certificate-in-certificate-chain-on-windows | # https://mattferderer.com/fix-git-self-signed-certificate-in-certificate-chain-on-windows | ||
git config --global http.sslVerify false | git config --global http.sslVerify false | ||
| + | git config --global http.https://server.com/.extraHeader "Authorization: Bearer XXXX" | ||
</pre> | </pre> | ||
| Line 86: | Line 117: | ||
* https://github.com/GitCredentialManager/git-credential-manager/blob/main/docs/wsl.md | * https://github.com/GitCredentialManager/git-credential-manager/blob/main/docs/wsl.md | ||
* https://github.com/GitCredentialManager/git-credential-manager/blob/main/README.md#linux-install-instructions | * https://github.com/GitCredentialManager/git-credential-manager/blob/main/README.md#linux-install-instructions | ||
| + | ===Installing=== | ||
| + | <pre class="code"> | ||
| + | brew install git-credential-manager | ||
| + | brew install --cask git-credential-manager | ||
| + | </pre> | ||
| + | |||
===Settings=== | ===Settings=== | ||
<pre class="code"> | <pre class="code"> | ||
| Line 115: | Line 152: | ||
git remote set-url origin git@github.com:username/repository-name.git ### For HTTPS-Connection | git remote set-url origin git@github.com:username/repository-name.git ### For HTTPS-Connection | ||
</pre> | </pre> | ||
| − | ==. | + | ==Ignore File (.gitignore)== |
| + | * https://www.atlassian.com/git/tutorials/saving-changes/gitignore#git-ignore-patterns | ||
<pre class="code"> | <pre class="code"> | ||
| − | folder/ | + | *.log ### Ignores all the files with the extension ".log", wherever they are. |
| − | .file | + | logs/ ### Ignores only the logs folder and its contents in the root directory. |
| − | + | **/logs ### Ignores any directory named logs and its contents, at any level in the repository. | |
| + | |||
| + | !logs/file.log ### un-ignore file.log if it has been ignored by a previous rule. | ||
</pre> | </pre> | ||
| − | =Commands= | + | =Commands-Core= |
| − | + | ==LifeCycle== | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | == | ||
<pre class="code"> | <pre class="code"> | ||
git clone https://github.com/samerhijazi/automation.git -b branchname ./local-folder | git clone https://github.com/samerhijazi/automation.git -b branchname ./local-folder | ||
git pull | git pull | ||
| + | git pull origin master | ||
git add README.md | git add README.md | ||
git commit -m "First commit" | git commit -m "First commit" | ||
git push -u origin master | git push -u origin master | ||
</pre> | </pre> | ||
| + | |||
==Initialisieren== | ==Initialisieren== | ||
<pre class="code"> | <pre class="code"> | ||
git init --bare # Initial Repo for remote | git init --bare # Initial Repo for remote | ||
git init # Initial Repo for local | git init # Initial Repo for local | ||
| − | + | -------------------------------------------------------- | |
git remote add origin https://samerhijazi@gitlab.com/samerhijazi/public.git ### Add URl for remote. | git remote add origin https://samerhijazi@gitlab.com/samerhijazi/public.git ### Add URl for remote. | ||
git remote set-url origin https://samerhijazi@gitlab.com/samerhijazi/public.git ### Change URL for remote. | git remote set-url origin https://samerhijazi@gitlab.com/samerhijazi/public.git ### Change URL for remote. | ||
</pre> | </pre> | ||
| − | |||
==Clone== | ==Clone== | ||
<pre class="code"> | <pre class="code"> | ||
git clone https://github.com/samerhijazi/automation.git -b branchname ./local-folder | git clone https://github.com/samerhijazi/automation.git -b branchname ./local-folder | ||
git clone ssh://ssh-w0000000@servername.com/www/htdocs/w0000000/repository/git/projectname.git | git clone ssh://ssh-w0000000@servername.com/www/htdocs/w0000000/repository/git/projectname.git | ||
| + | git clone -b <branchname> <remote-repo-url> | ||
| + | git clone --depth 1 --filter=blob:none <repository-url> <destination-directory>. ### Clone with leates commint | ||
</pre> | </pre> | ||
| + | |||
==Checkout== | ==Checkout== | ||
* HEAD reflects 'git checkout' | * HEAD reflects 'git checkout' | ||
| Line 167: | Line 204: | ||
git branch -r # list branches remote. | git branch -r # list branches remote. | ||
git branch -a # list branches all (local & remote). | git branch -a # list branches all (local & remote). | ||
| − | --- | + | --------------------------------------------------------------------------------------------- |
| − | git branch | + | git branch <branch_name> # Create a new Branch |
| − | git switch | + | git switch <branch_name> # Switch to branch |
| − | --- | + | --------------------------------------------------------------------------------------------- |
| − | git checkout | + | git checkout <branch_name> # checkout exiting branch |
| − | git checkout -b | + | git checkout -b <branch_name> # create a new branch from HEAD and then it checkout. |
| − | git checkout -b | + | git checkout -b <branch_name_new> <branch_name_exists> # create a new branch from $NAME_BRANCH_EXISIT and then checkout $NAME_BRANCH_NEW |
| − | --- | + | --------------------------------------------------------------------------------------------- |
| − | git push --set-upstream origin | + | git push --set-upstream origin <branch_name> # Push the <branch_name> branch and set the remote as upstream. |
</pre> | </pre> | ||
* Rename | * Rename | ||
| Line 184: | Line 221: | ||
* Delete | * Delete | ||
<pre class="code"> | <pre class="code"> | ||
| − | git branch -d | + | git branch -d <branch_name> # Delete a branch on local. |
| − | git push origin --delete | + | git push origin --delete <branch_name> # Delete a branch on remote. |
| − | git push origin : | + | git push origin :<branch_name> # Delete a branch on remote. |
</pre> | </pre> | ||
==Merge== | ==Merge== | ||
<pre class="code"> | <pre class="code"> | ||
| − | git checkout < | + | git branch -b <sandbox> ### Create and checkout a new branch with name "sandbox". |
| − | git merge --no-ff < | + | git commit -a -m "Add new code". ### Add new code in branch "sandbox". |
| − | git push | + | git switch <master> ### change to branch "master" where the merge will be done. |
| − | . | + | git pull ### pull changes from remote. |
| − | git merge --abort | + | git diff --name-only <sandbox> ### display only the names of the files that have differences between branches. |
| − | git reset --hard | + | git merge --no-ff <sandbox> ### merge branch "sandbox" in branch "master". |
| + | git tag -a v1.2 ### Tag the merge. | ||
| + | git push ### push changes in remote. | ||
| + | git branch -d <sandbox> ### Delete branch "sandbox". | ||
| + | --------------------------------------------------------------------------------------------------------------------------- | ||
| + | git merge --abort ### returen to the state before starting the merge. | ||
| + | git reset --hard ### roll back to the commit before the merge. | ||
</pre> | </pre> | ||
| + | |||
==Taging== | ==Taging== | ||
<pre class="code"> | <pre class="code"> | ||
| Line 232: | Line 276: | ||
==Reset== | ==Reset== | ||
<pre class="code"> | <pre class="code"> | ||
| + | ### reset your branch to match the remote branch, discarding local changes and commits: | ||
| + | git fetch origin | ||
| + | git reset --hard origin/<branch-name> # Reset your current local branch to origin's <branch-name> | ||
| + | ---------------------------------------------------------------------------------------------------- | ||
git reset HEAD~1 # Moves branch reference 1-commit back. | git reset HEAD~1 # Moves branch reference 1-commit back. | ||
| + | git reset HEAD~2 # Moves branch reference 2-commit back. | ||
git reset * # Undo "git add *" | git reset * # Undo "git add *" | ||
| − | + | ---------------------------------------------------------------------------------------------------- | |
| − | --- | ||
git revert HEAD # Undo last commit and make ready for remote. | git revert HEAD # Undo last commit and make ready for remote. | ||
</pre> | </pre> | ||
| + | |||
==Clean== | ==Clean== | ||
<pre class="code"> | <pre class="code"> | ||
| + | git clean -fdx | ||
git clean -f # Remove untracked files with force (-f). | git clean -f # Remove untracked files with force (-f). | ||
| − | |||
git clean -f -d # Remove untracked files and directories (-d). | git clean -f -d # Remove untracked files and directories (-d). | ||
git clean -f -x # Remove ignored and non-ignored files (-x). | git clean -f -x # Remove ignored and non-ignored files (-x). | ||
| Line 275: | Line 324: | ||
git restore . ### Restore working tree files. | git restore . ### Restore working tree files. | ||
git checkout . ### same as (git restore .). | git checkout . ### same as (git restore .). | ||
| + | </pre> | ||
| + | |||
| + | =Commands-Plus= | ||
| + | ==Tweaks== | ||
| + | <pre class="code"> | ||
| + | git rev-parse --show-toplevel ### Display root folder of a Git repository | ||
| + | </pre> | ||
| + | |||
| + | ==Clean History== | ||
| + | <pre class="code"> | ||
| + | git filter-branch --force --index-filter \ | ||
| + | 'git rm --cached --ignore-unmatch path/to/keystore.file' \ | ||
| + | --prune-empty --tag-name-filter cat -- --all | ||
| + | |||
| + | git reflog expire --expire=now --all | ||
| + | git gc --prune=now --aggressive | ||
| + | git push origin --force --all | ||
| + | git push origin --force --tags | ||
| + | </pre> | ||
| + | |||
| + | ==Submodule== | ||
| + | *https://www.atlassian.com/git/articles/core-concept-workflows-and-tips | ||
| + | <pre class="code"> | ||
| + | cd /path/to/parent-repo | ||
| + | git submodule add -b <branch> <repository> <path/to/submodule> | ||
| + | git submodule update ### Update submodule in the file ".gitmodules" | ||
| + | git submodule update --remote <path/to/submodule> ### | ||
| + | ------------------------------------------------------------------------------------ | ||
| + | git clone --recursive ### clone a Git repository that includes submodules. | ||
</pre> | </pre> | ||
| Line 288: | Line 366: | ||
* Hauptbraches: master & develop | * Hauptbraches: master & develop | ||
* Arbeitbraches: feature, release, hotfixes | * Arbeitbraches: feature, release, hotfixes | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
==Branch Release== | ==Branch Release== | ||
<pre class="code"> | <pre class="code"> | ||
| − | git checkout -b release-1.2 | + | git checkout -b release-1.2 ### create and checkout a new branch |
| − | git commit -a -m " | + | git commit -a -m "Add new code" |
... | ... | ||
git checkout master | git checkout master | ||
git merge --no-ff release-1.2 | git merge --no-ff release-1.2 | ||
git tag -a 1.2 | git tag -a 1.2 | ||
| − | + | git push | |
| − | git | ||
| − | |||
git branch -d release-1.2 | git branch -d release-1.2 | ||
</pre> | </pre> | ||
| − | + | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
==CICD== | ==CICD== | ||
<pre class="code"> | <pre class="code"> | ||
git --no-pager show -s --format=\'%ae\' # Disaply email address last commit | git --no-pager show -s --format=\'%ae\' # Disaply email address last commit | ||
git config user.name # Dispaly email address user | git config user.name # Dispaly email address user | ||
| + | </pre> | ||
| + | =Tweaks= | ||
| + | ==New Empty Branch== | ||
| + | <pre class="code"> | ||
| + | git switch --orphan <new branch> | ||
| + | git commit --allow-empty -m "Initial commit on orphan branch" | ||
| + | git push -u origin <new branch> | ||
</pre> | </pre> | ||
Latest revision as of 11:48, 23 July 2025
Contents
Ref.
- http://blog.jinlaixu.net/books/ProGit/en/index.html
- https://help.github.com/en/github/using-git
- https://www.atlassian.com/git/tutorials/setting-up-a-repository
- https://fedoraproject.org/wiki/Git_quick_reference
- https://www.git-tower.com/learn/git/ebook/en/command-line/advanced-topics/merge-conflicts
- https://hackernoon.com/understanding-git-index-4821a0765cf
- https://docs.gitlab.com/ee/topics/git/numerous_undo_possibilities_in_git/
- Git-Branching-Game: https://learngitbranching.js.org/
- https://www.toptal.com/software/trunk-based-development-git-flow
- Cheat-Sheet: https://github.github.com/training-kit/downloads/de/github-git-cheat-sheet/
- Cheat-Sheet: https://www.atlassian.com/git/tutorials/atlassian-git-cheatsheet
- https://www.gitignore.io
- JGit: https://www.vogella.com/tutorials/JGit/article.html
- JGit-Cookbook: https://github.com/centic9/jgit-cookbook/
Configuration
- Layers:
- Remote-Repo
- Local-Repo
- Staging-Area (Index)
- Working-Area (Directory)
- Stages: worktree, staged(index), HEAD
- HEAD refers to the latest commit in your current branch.
Shell & Git
Bash (.bashrc)
git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
PS1='\[\033[32m\][\w]\[\033[31m\] $(git_branch)\[\033[0m\]\n$ '
Zsh (.zshrc)
git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
setopt PROMPT_SUBST
PROMPT='[%F{green}%d%f]%F{red} $(git_branch)%f
$ '
Alternativ
git_branch() {
branch=$(git symbolic-ref --short HEAD 2> /dev/null)
if [[ $branch == "" ]]; then
:
else
echo '('$branch')'
fi
}
Global-Settings
- https://www.git-scm.com/book/en/v2/Appendix-C%3A-Git-Commands-Setup-and-Config
- https://www.scootersoftware.com/kb/vcs
git config --local --list # List Configs for repos. config file "repo/.git/config" git config --global --list # List Configs for global config file "~/.gitconfig" git config --system --list # List Configs for System config file "installed/git/etc/gitconfig" ----------------------------------------------------------------------------------------------------- git config --global --edit ### Edit Configs git config --global user.name ### Show config for user.name git config --global user.name "King" ### Set Value "King" to user.name git config --global --unset user.name ### Unset saved user.name git config --global --unset user.password ### Unset saved user.password
git config --global user.name "Samer Hijazi" git config --global user.email "samerhijazi@samerhijazi.net" ----------------------------------------------------------------------------------------------------- git config --global alias.ss 'status -s' git config --global alias.sm 'diff --name-only --diff-filter=M' git config --global alias.sd 'diff --name-only --diff-filter=D' git config --global alias.rs 'reset --hard origin/master' git config --global alias.cm 'commit -m' git config --global alias.cp '!git commit -a -m "Update $1" && git push' git config --global alias.xx '!git fetch origin && git reset --hard origin/master && git clean -fdx' git config --global alias.xx '!git checkout -- . && git reset --hard HEAD && git clean -fdx' ----------------------------------------------------------------------------------------------------- git config --global core.editor "code --wait" git config --global core.editor "nano" git config --global core.editor "vim" git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin" ----------------------------------------------------------------------------------------------------- git config --global http.proxy $PROXY_URL git config --global https.proxy $PROXY_URL git config --global http.https://devstack.samerhijazi.com/.extraHeader "Authorization: Bearer XXX" ----------------------------------------------------------------------------------------------------- git config --global core.autocrlf true # Working on Windows, Linux, macOS Maschines. git config --global core.safecrlf false # Disable showing Warnning for LF git config --global core.longpaths true git config --global core.compression 9 # compression for speed up push process. ----------------------------------------------------------------------------------------------------- git config --global diff.tool bc git config --global difftool.bc.path "C:/Program Files/Beyond Compare 4/bcomp.exe" ----------------------------------------------------------------------------------------------------- git config --global merge.tool bc git config --global mergetool.bc.path "C:/Program Files/Beyond Compare 4/bcomp.exe" ----------------------------------------------------------------------------------------------------- git config --global advice.addIgnoredFile false ### Turen off advice for adding Ignored files git config --global pull.rebase true ### Local changes will rebased on top of the changes pulled from the remote branch. ----------------------------------------------------------------------------------------------------- git commit --amend --reset-author
extras
# https://mattferderer.com/fix-git-self-signed-certificate-in-certificate-chain-on-windows git config --global http.sslVerify false git config --global http.https://server.com/.extraHeader "Authorization: Bearer XXXX"
Credential
Refs
- https://github.com/GitCredentialManager/git-credential-manager
- https://github.com/GitCredentialManager/git-credential-manager/blob/main/docs/wsl.md
- https://github.com/GitCredentialManager/git-credential-manager/blob/main/README.md#linux-install-instructions
Installing
brew install git-credential-manager brew install --cask git-credential-manager
Settings
git config --global --edit git config --global credential.helper cache ### keeps credentials in memory for a 15min (default cache timeout). git config --global credential.helper 'cache --timeout=3600' ### keeps credentials in memory for a 36min. git config --global credential.helper store ### save entered password in (~/.git-credentials) by default. git config --global credential.helper 'store --file ~/.my-credentials' git config --global credential.helper '/usr/local/share/gcm-core/git-credential-manager-core' ### Set "Git Credential Manager" on Linux. git config --global credential.credentialStore secretservice ### GUI Storage für Linux git config --global credential.helper '/mnt/c/git/mingw64/bin/git-credential-manager-core.exe' ### Set "Git Credential Manager" on WSL.
ssh
type %userprofile%\.ssh\id_rsa.pub | clip cat %userprofile%\.ssh\id_rsa.pub | clip
chmod 600 ~/.ssh/key_github eval "$(ssh-agent -s)" ssh-add ~/.ssh/key_github ssh -T git@github.com
ssh -T git@gitlab.com ssh -T git@bitbucket.org git remote set-url origin https://github.com/username/repository-name.git ### For SSH-Connection git remote set-url origin git@github.com:username/repository-name.git ### For HTTPS-Connection
Ignore File (.gitignore)
*.log ### Ignores all the files with the extension ".log", wherever they are. logs/ ### Ignores only the logs folder and its contents in the root directory. **/logs ### Ignores any directory named logs and its contents, at any level in the repository. !logs/file.log ### un-ignore file.log if it has been ignored by a previous rule.
Commands-Core
LifeCycle
git clone https://github.com/samerhijazi/automation.git -b branchname ./local-folder git pull git pull origin master git add README.md git commit -m "First commit" git push -u origin master
Initialisieren
git init --bare # Initial Repo for remote git init # Initial Repo for local -------------------------------------------------------- git remote add origin https://samerhijazi@gitlab.com/samerhijazi/public.git ### Add URl for remote. git remote set-url origin https://samerhijazi@gitlab.com/samerhijazi/public.git ### Change URL for remote.
Clone
git clone https://github.com/samerhijazi/automation.git -b branchname ./local-folder git clone ssh://ssh-w0000000@servername.com/www/htdocs/w0000000/repository/git/projectname.git git clone -b <branchname> <remote-repo-url> git clone --depth 1 --filter=blob:none <repository-url> <destination-directory>. ### Clone with leates commint
Checkout
- HEAD reflects 'git checkout'
git checkout main # checkout main-Branch@HEAD git checkout main^ # moves HEAD 1-commit back from main-Branch git checkout main~4 # moves HEAD 4-commits back from main-Branch git branch -f main HEAD~3 # moves (by force) the main-Branch 3-commits back from HEAD.
Branch
- Checkout
git branch # list branches local. git branch -r # list branches remote. git branch -a # list branches all (local & remote). --------------------------------------------------------------------------------------------- git branch <branch_name> # Create a new Branch git switch <branch_name> # Switch to branch --------------------------------------------------------------------------------------------- git checkout <branch_name> # checkout exiting branch git checkout -b <branch_name> # create a new branch from HEAD and then it checkout. git checkout -b <branch_name_new> <branch_name_exists> # create a new branch from $NAME_BRANCH_EXISIT and then checkout $NAME_BRANCH_NEW --------------------------------------------------------------------------------------------- git push --set-upstream origin <branch_name> # Push the <branch_name> branch and set the remote as upstream.
- Rename
git branch -m $NAME_OLD $NAME_NEW # To rename a local branch git push origin -u $NAME_OLD :$NAME_NEW # To rename a remote branch
- Delete
git branch -d <branch_name> # Delete a branch on local. git push origin --delete <branch_name> # Delete a branch on remote. git push origin :<branch_name> # Delete a branch on remote.
Merge
git branch -b <sandbox> ### Create and checkout a new branch with name "sandbox". git commit -a -m "Add new code". ### Add new code in branch "sandbox". git switch <master> ### change to branch "master" where the merge will be done. git pull ### pull changes from remote. git diff --name-only <sandbox> ### display only the names of the files that have differences between branches. git merge --no-ff <sandbox> ### merge branch "sandbox" in branch "master". git tag -a v1.2 ### Tag the merge. git push ### push changes in remote. git branch -d <sandbox> ### Delete branch "sandbox". --------------------------------------------------------------------------------------------------------------------------- git merge --abort ### returen to the state before starting the merge. git reset --hard ### roll back to the commit before the merge.
Taging
git tag # List tages git tag -a v2021.09.03 -m "Messages" # Add tag git tag -m $TAG_OLD $TAG_NEW # Rename tag. git push origin $TAG # Push a current tag to remote. git push origin --tags # Push all tags to remote. --- git fetch # Fetch remote All tags. git tag -d $TAG_TO_DELETE # Delete a tag on local. git push origin :refs/tags/$TAG_TO_DELETE # Delete a tag on remote variant 1. git push origin --delete $TAG_TO_DELETE # Delete a tag on remote variant 2. --- git tag -d $(git tag -l) # Delete all local tags. git push origin --delete $(git tag -l) # Delete all remote tags.
Remove & Rename
git checkout <branch-name> git rm -r <file-to-remove> # remove file git mv <filename-old> <filename-new> # rename file git clean -fd # remove directories forced git commit -m "My Massage" git push origin <branch-name>
Stash
git stash # Save all local changes und remove them from workspace. git fetch # fetch from the default remote, origin
Reset
### reset your branch to match the remote branch, discarding local changes and commits: git fetch origin git reset --hard origin/<branch-name> # Reset your current local branch to origin's <branch-name> ---------------------------------------------------------------------------------------------------- git reset HEAD~1 # Moves branch reference 1-commit back. git reset HEAD~2 # Moves branch reference 2-commit back. git reset * # Undo "git add *" ---------------------------------------------------------------------------------------------------- git revert HEAD # Undo last commit and make ready for remote.
Clean
git clean -fdx git clean -f # Remove untracked files with force (-f). git clean -f -d # Remove untracked files and directories (-d). git clean -f -x # Remove ignored and non-ignored files (-x). git clean -f -X # Remove ignored files only (-X).
Remote
git remote -v # View remotes. git remote add $REMOTE_NAME $REMOTE_PATH # Add remote. git remote rm $REMOTE_NAME # Delete remote. git remote rename $REMOTE_NAME_OLD $REMOTE_NAME_NEW # Change remote name from 'origin' to 'destination' --- git remote prune origin --dry-run git remote prune origin
Fetch & Pull & Push
git fetch # Download new data (commits, files, and refs.) from a remote-repository into local-repository. git fetch --prune # Remove remote branches that not longer have a counterpart. --- git pull # Download new data (commits, files, and refs.) from a remote-repository into local-repository and integrate the new data in working-files. git pull --rebase origin master # get changes from remote master. And commit it for local/user commit. --- git push # Upload local-repository content to a remote-repository. git push --force-with-lease # To avoid overwrite history from rebase, Lease entsure that history dokumented.
Reset local workspace
git clean -fxd ### Clean ignored and non-ignored files/directoryes. git reset –-hard ### Reset current HEAD to the specified state. git restore . ### Restore working tree files. git checkout . ### same as (git restore .).
Commands-Plus
Tweaks
git rev-parse --show-toplevel ### Display root folder of a Git repository
Clean History
git filter-branch --force --index-filter \ 'git rm --cached --ignore-unmatch path/to/keystore.file' \ --prune-empty --tag-name-filter cat -- --all git reflog expire --expire=now --all git gc --prune=now --aggressive git push origin --force --all git push origin --force --tags
Submodule
cd /path/to/parent-repo git submodule add -b <branch> <repository> <path/to/submodule> git submodule update ### Update submodule in the file ".gitmodules" git submodule update --remote <path/to/submodule> ### ------------------------------------------------------------------------------------ git clone --recursive ### clone a Git repository that includes submodules.
Git branching model
- src: http://blog.plasticscm.com/2017/04/how-we-do-trunk-based-development.html
- src: https://cloud.google.com/solutions/devops/devops-tech-trunk-based-development
- src: https://medium.com/safetycultureengineering/trunks-are-not-just-for-trees-from-git-flow-to-trunk-based-development-949d580697ef
- src: https://trunkbaseddevelopment.com/
- src: https://nvie.com/posts/a-successful-git-branching-model/
- src: https://medium.com/@patrickporto/4-branching-workflows-for-git-30d0aaee7bf
- src: https://guides.github.com/introduction/flow/index.html
- src: https://www.youtube.com/watch?v=1SXpE08hvGs
- Hauptbraches: master & develop
- Arbeitbraches: feature, release, hotfixes
Branch Release
git checkout -b release-1.2 ### create and checkout a new branch git commit -a -m "Add new code" ... git checkout master git merge --no-ff release-1.2 git tag -a 1.2 git push git branch -d release-1.2
CICD
git --no-pager show -s --format=\'%ae\' # Disaply email address last commit git config user.name # Dispaly email address user
Tweaks
New Empty Branch
git switch --orphan <new branch> git commit --allow-empty -m "Initial commit on orphan branch" git push -u origin <new branch>