git - konradmayer/knowledge-base GitHub Wiki

Ressources

https://git-scm.com/book/en/v2
https://rogerdudler.github.io/git-guide/
http://think-like-a-git.net
https://github.com/UnseenWizzard/git_training
https://rachelcarmena.github.io/2018/12/12/how-to-teach-git.html
https://docs.gitlab.com/ee/topics/git/useful_git_commands.html
https://github.com/k88hudson/git-flight-rules
https://github.com/rstudio-conf-2020/what-they-forgot/blob/master/materials/git-and-github-daily-workflows.pdf
https://rstudio-conf-2020.github.io/what-they-forgot/materials/git-and-github-early-usage.pdf \
https://peerj.com/preprints/3159/
https://learngitbranching.js.org/

Rebasing:
https://dev.to/maxwell_dev/the-git-rebase-introduction-i-wish-id-had

Subtrees:
https://makingsoftware.wordpress.com/2013/02/16/using-git-subtrees-for-repository-separation/
https://www.oasys.net/fragments/git-subtree-split/
https://git-memo.readthedocs.io/en/latest/subtree.html \

Config

git config --global user.name "Konrad Mayer"
git config --global user.email [email protected]
git config --global core.editor "code --wait"
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait $MERGED'
git config --global diff.tool vscode
git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'

check ssh connection to gitlab:

draw example git graphs

https://codepen.io/nicoespeon/pen/arqPWb?editors=1010

Aliases

better git log

https://coderwall.com/p/euwpig/a-better-git-log

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

modified to show all branches

git config --global alias.lga "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --all"

search commit

https://opensource.com/article/20/11/git-aliases

git config --global alias.se '!git rev-list --all | xargs git grep -F'

shortcuts

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status

unstage

git config --global alias.unstage 'reset HEAD --'

Commands

get file from different branch

git switch branch-to
git restore --source branch-from -- file.txt

compare file on different branches

git diff mybranch master -- myfile.cs

cherry-pick specific commit

git cherry-pick <commit>

be aware of the consequences: https://stackoverflow.com/a/881014

stashing

git stash

add context info:

git stash save "context info text here"

get stash back again:

git stash pop

or a specific stash

git stash list 
git stash pop stash@{2}

delete commit back to a specific commit

https://stackoverflow.com/a/1338744: git reset --hard WILL DELETE YOUR WORKING DIRECTORY CHANGES. Be sure to stash any local changes you want to keep before running this command

git reset --hard <sha1-commit-id>

set new remote url

git remote -v
git remote set-url origin https://github.com/USERNAME/REPOSITORY.git

inspect old code in a separate branch

git checkout -b branchname somehash

abort a merge

git merge --abort

gitk

compare branches:

gitk master..featurebranch

prune

https://stackoverflow.com/a/5751635

git fetch -p

compare uncommited changes with a commit/branch

https://stackoverflow.com/a/31703666/10943838

git diff <commit-ish>:./ -- <path>

subtrees

Extract a subfolder from existing project and put it in a separate repo:

# in main project create branch with commits related to the files in subfolder
git subtree split --prefix=path/to/subdirectory -b new-branch
# create new repo directory for the "extracted" code
mkdir ~/newdir/
cd ~/newdir
git init
# pull the split branch to the new local repo
git pull ~/path/to/main/project new-branch
# push new local repo to new remote
git remote add origin [email protected]:/kmayer/new-remote.git
git push -u origin master
# delete branch and associated subdir in main project
git branch -D new-branch
git rm -r path/to/subdirectory/
# now commit changes
# add new repo in `~/newdir/` as subtree to main project
# add subtree remote repo
git remote add subtree-remote ssh://[email protected]/kmayer/new-remote.git
# pull remote to subtree (use `--squash` if commits from subtree should appear as a single commit in main projects history, omit to keep all commits)
git subtree add --prefix=path/to/subdir --squash subtree-remote master

Whenever the subproject remote changes, pull commits to subtree using

git subtree pull --prefix=path/to/subdir --squash subtree-remote master

move last commits to new branch

https://stackoverflow.com/a/1628584/10943838

Stash uncommites changes or they will be lost!

move to an existing branch

git checkout existingbranch
git merge master
git checkout master
git reset --hard HEAD~3 # Go back 3 commits. You *will* lose uncommitted work.
git checkout existingbranch

move to new branch

git branch newbranch      # Create a new branch, saving the desired commits
git reset --hard HEAD~3   # Move master back by 3 commits (Make sure you know how many commits you need to go back)
git checkout newbranch    # Go to the new branch that still has the desired commits

archive a branch

by tagging and removing it: https://stackoverflow.com/a/1309934 (resp https://stackoverflow.com/a/42232899)

git tag archive/<branchname> <branchname>
git branch -D <branchname>

restore it later:

git checkout -b <branchname> archive/<branchname>
⚠️ **GitHub.com Fallback** ⚠️