Snippets of Git

Page content

Completely delete submodule from the repository

Here is the graceful memo.
I copy&paste it to here.

To remove a submodule you need to:

  • Delete the relevant section from the .gitmodules file.
  • Stage the .gitmodules changes git add .gitmodules
  • Delete the relevant section from .git/config.
  • Run git rm –cached path_to_submodule (no trailing slash).
  • Run rm -rf .git/modules/path_to_submodule (no trailing slash).
  • Commit git commit -m “Removed submodule ”
  • Delete the now untracked submodule files rm -rf path_to_submodule

Do you forgot to clone with --recursive ?

Do it in the Git repo.

git submodule update --init --recursive

Delete all .DS_Store files in a Git repository (macOS)

https://stackoverflow.com/questions/107701/how-can-i-remove-ds-store-files-from-a-git-repository

find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch

Check status before commit

git status -v

Cancel last commit - git reset

git reset --soft HEAD~1

Squash

git reset --soft HEAD~3
git commit

Create branch and switch to it

git checkout -b new-branch-name

Relocate repository (Shoule be reviewed, don’t follow)

git clone --mirror {{ original_repo }}
cd {{ original_repo }}
git push --mirror git@{{ server_name }}:{{ group_name }}/{{ repository_name }}.git

rebase and merge

https://sdqweb.ipd.kit.edu/wiki/Git_pull_--rebase_vs._--merge

good take away.

git config --global pull.rebase true

Update submodule

git submodule update --rebase --remote

Change submodule URL from HTTPS to SSH

edit .gitmodule
git submodule sync
git submodule update --init --recursive --remote
git add .gitmodule
git commit
git push

Clone only a specific branch

git clone -b branch_name --single-branch git@github.com:atlex00/myrepo.git

Remove remote-tracking branch in the local

  hello-world
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/hello-world
  remotes/origin/main
git branch -rd origin/hello-world
  hello-world
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/main

Sync a local branch and a remote branch

git branch --set-upstream-to=origin/error-handling error-handling

Want to change the content of last commit?

git reset --soft HEAD~

You can change commit contents and the last commit message.