Skip to content

Latest commit

 

History

History
90 lines (61 loc) · 2.19 KB

git.md

File metadata and controls

90 lines (61 loc) · 2.19 KB

git cheat sheet

Exporting a file from a repository and importing it into another while keeping its history

https://github.com/CNG/dotfiles/blob/master/files/zsh/config/git/functions/git-import

# creates patches from $object (either file or dir) including history
cd some_repo
git format-patch --thread -o "$temp_dir" --root -- "$object"

# replay patches into other repo
cd other_repo
git am "$temp_dir"/*.patch || git am --abort

Clone with submodules

git clone --recursive https://host/dir/repo.git

Clone the submodule if it was initially forgotten

cd cloned-repo
git submodule update --init --recursive

Show all your remotes

git remote -v

Renaming a remote

git remote rename old_name new_name

Set user name and email

Globally

git config --global user.name "John Doe"
git config --global user.email [email protected]

For current repository

git config --local user.name "John Doe"
git config --local user.email [email protected]

Rewrite git access from SSH to HTTPS

Rewritting the remote URL is especially useful when using submodules since submodules usually keep their remote regardless how the parent got cloned (setting can also be done per repo).

# rewrite SSH
git config --global url.https://gitserver/.insteadOf ssh://git@gitserver/

# rewrite git protocol
git config --global url.https://gitserver/.insteadOf git@gitserver/

Change author (name/email) of last commit

git commit --amend --author "name <[email protected]>"

Rebase including initial commit

git rebase -i --root

Rebase starting with specific commit

Rebase commits starting and including COMMIT on top of branch:

git rebase --onto branch SHA_OF_COMMIT^

The ^ attached to SHA_OF_COMMIT is crucial so that COMMIT is also included in the rebase.