Git notes

First-Time Git Setup

# find .gitconfig location
git config --list --show-origin

# set username and email address
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com

# configure default text editor
git config --global core.editor code --wait

# set vscode as default merge tool
git config --global merge.tool code

# get git config value by key
git config --get user.name
git config --get user.email
git config --get core.editor
git config --get merge.tool
git config --get core.autocrlf
git config --get core.eol

# list git config values
git config --list

# setup git alias using git config
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
# git unstage filename
git config --global alias.unstage 'reset HEAD --'
# git last
git config --global alias.last 'log -1 HEAD'
# git visual
git config --global alias.visual '!gitk'

git-branch - List, create, or delete branches

# list local branches
git branch
git branch --list


# list remote branches
git branch -r


# list local and remote branches
git branch -a


# delete local branch called "newbranch" that is fully merged
git branch -d newbranch


# force delete local branch called "newbranch" that isn't fully merged
git branch -D newbranch

git-checkout - Switch branches or restore working tree files

# switch to branch called "master"
git checkout master

# create a new branch called "newbranch" and switch to that branch
git checkout -b newbranch

# create a new branch called "newbranch" if it doesn't exist
# otherwise, reset branch and switch to that branch
git checkout -B newbranch

# undo change to existing files
git checkout -- .

git-clean - Remove untracked files from the working tree

-q, --quiet           do not print names of files removed
-n, --dry-run         dry run
-f, --force           force
-i, --interactive     interactive cleaning
-d                    remove whole directories
-e, --exclude 
                      add  to ignore rules
-x                    remove ignored files, too
-X                    remove only ignored files
# git clean interactive
git clean -i
git clean --interactive

# git clean dry-run
git clean -n
git clean --dry-run

# git clean force
git clean -f
git clean --force

# remove untracked files and directories
git clean -fd
git clean --force -d

# remove all untracked files
git clean -fx
git clean --force -x

# remove ignored files only
git clean -fX
git clean --force --quiet -X

# remove all untracked files/directories
git clean -fdx
git clean --force -dx

# remove untracked files/directories and ignored files
git clean -fdX
git clean --force -dX

# remove untracked files/directories, ignored files and subrepository
# note using -f option twice
git clean -fdX -f
git clean --force -dX --force

git-config - Get and set repository or global options

# set git username
git config --global user.name "John Doe"

# set git email
git config --global user.email johndoe@example.com

# set git default editor
git config --global core.editor "code --wait"


# set merge.renameLimit to 99999
git config --global merge.renameLimit 99999

# unset merge.renameLimit limit
git config --unset merge.renameLimit

git-fetch - Download objects and refs from another repository

# fetch branches and/or tags and prune deleted remote branch
git fetch -p

git-init - Create an empty Git repository or reinitialize an existing one

# create an empty git repo
# in other words, a .git directory with subdirectories for objects, refs/heads, refs/tags, and template files
git init

# create an empty git repo without the .git directory
# in other words, just the subdirectories for objects, refs/heads, refs/tags, and template files
git init --bare

git-log - Show commit logs

# show git commit logs
git log --oneline
git log --pretty=oneline --abbrev-commit


# show git commit logs limit 10
git log --oneline -n 10

git-merge - Join two or more development histories together

# merge changes from commit id a8b29192fe
git merge a8b29192fe

git-push - Update remote refs along with associated objects

git push origin master

# add upstream tracking reference for every branch that is up to date or sucessfully pushed
git push -u origin master

# force update a remote ref that is not an ancestor of the local ref used to overwrite it
git push --force origin master

# delete remote branch called "newbranch"
git push origin --delete newbranch

git-rebase - Reapply commits on top of another base tip

# make a list of the commits which are about to be rebased
git rebase -i HEAD~2

# abort rebase
git rebase --abort

# restart the rebasing process after having resolved a merge conflict
git rebase --continue

git-remote - Manage set of tracked repositories

# show remote name and urls
git remote -v


# remove a remote named 'origin'
git remote remove origin
git remote rm origin


# rename a remote named 'origin' to 'deploy'
git remote rename origin deploy


# add a remote named 'origin' for the repository at https://github.com/user/repo.git
git remote add origin https://github.com/user/repo.git

# prune deleted remote branches
git remote prune

# report which branches will be pruned without actually pruning
git remote prune -n

git-reset - Reset current HEAD to the specified state

# reset the current branch head to commit id db6574a24c
git reset db6574a24c
git reset --mixed db6574a24c

# does not touch the index file or working tree, but resets the head to commit id db6574a24c
git reset --soft db6574a24c

# shorthand for git reset --soft HEAD^1 - resets the head to the commit's first parent
git reset --soft HEAD^
# shorthand for git reset --soft HEAD~1 - resets the head to the commit's first parent
git reset --soft HEAD~

# resets the head to the commit's first parent's first parent
git reset --soft HEAD^2
# resets the head to the commit's secoond parent (commits can have two parents when there is a merge)
git reset --soft HEAD~2

# resets the index and working tree. any changes to tracked files in the working tree are discarded
git reset --hard db6574a24c

git-stash - Stash the changes in a dirty working directory away

# list stash entries
git stash list

# stash local changes (include untracked)
git stash push -u
git stash push --include-untracked

# stash local changes (include untracked with message)
git stash push -u -m 'some stash message'
git stash push --include-untracked --message 'some stash message'

# stage and then stash local changes (but keep staged changes intact)
git add .
git stash push -k -m 'some stash message'
git stash push --keep-index --message 'some stash message'

# apply the latest stash but do not delete the stash
git stash apply

# apply the latest stash and delete the stash
git stash pop

# drop stash named 'stash@{0}'
git stash drop stash@{0}

git-tag - Create, list, delete or verify a tag object signed with GPG

git supports two types of tags: lightweight and annotated

# list your tags
git tag

# list tags that match pattern
git tag -l "v1.8.5*"

# show tag data
git show v1.4

# add annotated tag called 'lesson10'
git tag -a lesson10
git tag -a lesson10 -m "Components Responsibilities"

# add lightweight tag called 'lesson10' to remote
git tag lesson10

# add (annotated) tag later after commit
git log --oneline
git tag -a v1.2 9fceb02

# push tag called 'lesson10' to remote
git push origin lesson10

# push all tags to remote
git push origin --tags

# delete local tag called 'lesson10'
git tag -d lesson10

# delete remote tag called 'lesson10'
git tag -d lesson10
git push origin --delete lesson10
# or
git push origin :refs/tags/lesson10

# checkout tag
git checkout lesson10

# checkout tag and create new branch
git checkout -b debuglesson10 lesson10
# Switched to a new branch 'debuglesson10'

miscellaneous notes

How can I determine the URL that a local Git repository was originally cloned from?

# get remote url
git config --get remote.origin.url

# get full output
git remote show origin

How to migrate from Bitbucket to GitHub

# step 1 - check out the existing repo from bitbucket
git clone git@bitbucket.org:kimbaudi/project.git

# step 2 - add the new github repo as upstream remote
cd project
git remote add upstream git@github.com:kimbaudi/project.git

# step 3 - push all branches
git push upstream master
git push --tags upstream

# step 4 - delete the bitbucket repository

How to replace master branch in Git, entirely, from another branch?

# make seotweaks the master branch
git checkout seotweaks
git merge -s ours master
git checkout master
git merge seotweaks

How to set and unset merge rename limit

# set merge rename limit
git config merge.renameLimit 999999

# unset merge rename limit
git config --unset merge.renameLimit
Source

© 2020 | Paul Kim