Skip to content

Git

Use SSH Key for Any Git Command to Git Repo

export GIT_SSH_COMMAND='ssh -i ~/.ssh/id_ed25519-github-xxx'
git ...

Fork a public repo into private Github repo

# create a private repo on github
git clone https://github.com/yourname/private-repo.git

git remote add public https://github.com/exampleuser/public-repo.git
git pull public master

# copy your changed files to this git, then select your private upstream
git add .
git push

Update upstream url

git remote set-url origin

List upstream url

git remote -v

Remove existing submodule

git submodule deinit <submodule_path>
rm .git/index
rm -rf .git/modules

Rename a branch to another, then delete the old branch

E.g. I want to rename next branch to develop, then delete next branch

git pull next
git checkout -b develop
git push develop

If the old branch next is set as the default branch in gitlab, you have to set another branch such as develop or master as the default branch. Otherwise you cannot delete the old branch using the command below.

# delete old branch
git push --delete origin next

Get most recent git commit messages

TIP

You can also check which branch you are at now

# get most recent last 5 git commits
git log -5

Git clone using ssh instead of https

Gen keypair and copy/paste public key to Gitlab/SSHKey menu.

ssh-keygen && cat ~/.ssh/id_rsa.pub

For existing git repo

git remote -v
git remote remove origin
git remote add origin ssh://[email protected]:2222/sys-dev/file-e2ee-api-server.git
git fetch

For new repo

git clone ssh://[email protected]:2222/sys-dev/file-e2ee-api-server.git

Note: We use ssh://git@...:2222 is because our gitlab VM is listening on port 2222 and not the default 22.

Git add submodule using ssh

git submodule add ssh://[email protected]:2222/sys-dev/fecb-encryption-kit.git
git submodule update

Git repo clone + save credentials

git config --global user.name "your_github_username"
git config --global user.email "your_github_email"
git config -l

git clone ...

git config --global credential.helper cache

Delete cache record

git config --global --unset credential.helper
git config --system --unset credential.helper

Git commit existing change to new branch

git switch -c <new-branch>

Ignore ssl

git config --global http.sslVerify false # Do NOT do this!

Set credentials

git config credential.helper store

Then git pull and enter password. Git will not prompt for password again.

Remove git submodules

This will remove the subproject commit and .gitmodules

git rm <path/to/submodules>

Clone with submodules

Method 1: One liner

git clone https://gitlab.com/<group>/<repo> -b develop --recursive

Method 2: Three Liners

git clone https://gitlab.com/group/repo -b develop
git submodule init
git submodule update --recursive

Add a new repo as submodule in existing repo

git submodule add ...

Force revert back to speicific commit

git reset --hard <commit you wanted to go back to></commit>

# force push
git push -f

Change commit author name

git rebase -i -p <last good commit>

Then change the prefix into edit

Then

git commit --amend --author="John Doe <[email protected]>" --no-edit
git rebase --continue

You can also reorder the commit.