Git

Page content

Some Git Commands

Customizing Git

Switch from “Master” to Main globally

git config --global init.defaultBranch main

Merge two Repos “merge unrelated histories”

git pull origin master --allow-unrelated-histories
git push
git pull

add local Folder and Push to Upstream

echo "# test" >> README.md
git init
git config init.defaultBranch main
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin git@github.com:stoege/test.git
git push -u origin main

Find deleted file, sort uniq

git log --all --pretty=format: --name-only --diff-filter=D | sort -u
bla
bla.yml
doit.sh
files.conf.j2
...

Find deleted File

git log --diff-filter=D --summary

commit abcecadce91af3814662fa6a04d0f12e361f0574
Date:   Sun May 31 23:19:59 2020 +0200

    update

 delete mode 100644 master/sed.tcpdump

commit 81ae58d70c27d02eb2f65beed4fe0b571073f087
Date:   Fri May 29 16:06:14 2020 +0200

    update

Restore deleted File

git checkout 81ae58d70c27d02eb2f65beed4fe0b571073f087 sed.tcpdump

Remove Sensitive Data

git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch .geheimesfile' \
--prune-empty --tag-name-filter cat -- --all

git push origin --force --all

git push origin --force --tags

Remove last Commit

will remove the last Commit from your current branch

git reset --soft HEAD~1

Search Keyword in File through the history

looking for the word “keyword” in myfile.sh, crawling all the git commits

git log -G'keyword' -- myfile.sh

Remove Commited File from Version Control

you have some (log) files under version control. you may wanna update .gitignore, but these already commited Files do not get removed from Version Control automatically

echo 'container/prod/kuma/data/kuma.db-shm' >> .gitignore
git rm --cached container/prod/kuma/data/kuma.db-shm
git status

-> –cached removes the file from the repository, but keep it locally

List Files which are excluded via .gitignore

git ls-files --other --ignored --exclude-standard

Any Comments ?

sha256: 9eedfe15d3b39bea752347225095102d5b50645926c26ac777e8c6488f2f7529