Skip to content

Aaron-Zhao/git-cookbook

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 

Repository files navigation

Git Cookbook

A repository for commonly used git commands.

Git config edit, global, local, list, caching pwd

$ git config --global -e
$ git config --global user.name "User Name"

$ git config --global user.email "user_email@domain.com"
$ git config user.name "User Name"

$ git config user.email "user_email@domain.com"
$ git config -l
# Windows
$ git config --global credential.helper wincred
# Mac
$ git config --global credential.helper osxkeychain
# Linux
$ git config --global credential.helper cache

Initializing

$ git init

Adding a remote source

$ git remote add <aliasName> <remoteAddress>
$ git remote add origin "git@github.com:Aaron-Zhao/git-cookbook.git"

Create a branch

$ git branch <newBranchName>

Checkout a branch

$ git checkout <branchName>

Create a branch and check it out right after that

$ git checkout -b <newBranchName>

Checkout a remote branch

$ git fetch
$ git checkout <branchName>

Rename a branch

$ git branch -m <newBranchName>
$ git branch -m <oldBranchName> <newBranchName>

Rename a remote branch

# rename locally
$ git branch -m <oldBranchName> <newBranchName>
# delete old remote branch
$ git push <remote> :<oldBranchName>
# push new branch to remote
$ git push <remote> <newBranchName>

Show branches

$ git branch

Adding a file and commit with comment

$ git add <file>
$ git commit -m 'Your comment'

Adding modified files and commit with comment

$ git add .
$ git commit -m 'Your comment'
above can be shorthanded with just
$ git commit -am 'Your comment'

Adding all (modified and new) files and commit with comment

$ git add --all
$ git commit -m 'Your comment'

Amending previous commit comment

$ git commit --amend -m 'New commit message'

Checking status

$ git status

Push your branch to remote

$ git push <remoteAliasName> <localBranchName>

.gitignore

# To remove all ignored but staged files
git rm -r --cached .
git add .
git commit -m "Removing all files in .gitignore"

Ignoring files (not to be checked in git), refer to this docs for more details.


Undo

# To UNDO local unstaged file changes only (for tracked files)
$ git checkout .

# To UNDO a local unstaged file changes
$ git checkout -- <file>

# To UNDO staged file changes or new files only
$ git reset .
$ git reset <file>
$ git rm --cached <file>
$ git rm -r --cached <folder>

# To UNDO local file changes (stage or unstaged) but NOT REMOVE your last commit, then use
$ git reset --hard

# To UNDO local file changes AND REMOVE your last commit, then use
$ git reset --hard HEAD~

# To KEEP local file changes and REMOVE ONLY your last commit, then use
$ git reset --soft HEAD~

# undo x number of commits
$ git reset HEAD~<x>

# push undo to remote
$ git revert HEAD

# remove untracked files
# to see what files will be deleted
$ git clean -n

# delete untracked files
$ git clean -f

# delete directories
$ git clean -fd

# delete ignored files
$ git clean -fX

# delete ignored and non-ignored files
$ git clean -fx

Revert

# This create a new commit that reverts all changes made by <commit>
$ git revert <commit>

Working with remote

# list
$ git remote
$ git ls-remote

# verbose
$ git remote -v

# add a new remote repository of your project
$ git remote add <aliasName> <remoteAddress>

# removing an existing remote alias
$ git remote rm <aliasName>

# rename remote alias
$ git remote rename <oldAliasName> <newAliasName>

# update an existing remote URL
$ git remote set-url <aliasName> <NewRemoteAddress>

# clean up remote branches in local
$ git remote prune origin

# test without doing it
$ git remote prune origin --dry-run

# push your new branch to a remote repository
$ git push <aliasName> <branchName>

# pull a remote branch 
$ git pull <aliasName> <branchName>

# download new branches and data from a remote repository
$ git fetch <aliasName>

# merge alias to your branch
$ git merge [aliasName/branchName]

Working with branches

# show local, [remote], [all] branches
$ git branch [-r] [-a]

# create and checkout a branch
$ git checkout -b <branchName>

# rename
$ git branch -m <branchName> <newBranchName>

# get back deleted branch before it gets clean up (30 days after deletion)
$ git reflog
$ git branch <newBranchName> <deletedBranchId>

# switching between 2 branches
$ git checkout -

# delete branch with possible error message
$ git branch -d <branchName>

# delete branch without questioning
$ git branch -D <branchName>

# delete remote branch
$ git push -d <remote> <branchName>

# push local branch to remote
$ git push <remote> <branchName>

# force push local branch to remote, if you're the only one working on the branch
$ git push -f <remote> <branchName>

# force with lease otherwise
$ git push --force-with-lease <remote> <branchName>

# pull remote branch to local
$ git pull origin <branchName>

# list branches merged
$ git branch -a --merged

# list branches not merged
$ git branch -a --no-merged

# merge a branch
$ git merge <branchName>

# merge a branch with message
$ git merge <branchName> -m "Your merge message"

# rebase onto a branch
$ git rebase <branchName>

# squash x number of commits into one, follow the instructions to complete it
$ git rebase -i HEAD~<x>

# do if you want update the timestamp after squashing
$ git commit --amend --date="now"

# abort
$ git rebase --abort
$ git merge --abort

# show files in conflicts
$ git diff --name-only --diff-filter=U
# add it to an alias so you can reuse it
$ git config --global alias.conflicts "diff --name-only --diff-filter=U"
$ git conflicts

# to resolve rebase conflicts, first resolve any code conflicts by either accepting their or your changes
# then add those files to staging, you can see them by $ git status
$ git add .
# after that continue rebasing
$ git rebase --continue
# if all you code changes are replaced by their changes, skip rebase and apply your changes manually
$ git rebase --skip
# update remote with
# force push local branch to remote, only if you're the only one working on the branch
$ git push -f <remote> <branchName>
# force with lease otherwise
$ git push --force-with-lease <remote> <branchName>

# revert rebase
# reference log
$ git reflog
# reset to the place right before your rebase
$ git reset --hard HEAD@{x}

Strategies for grandchildren branches to rebase onto a grandparent branch (with parent merged)

# sometimes you would want to branch off a feature branch to do more work that depends on it, while waiting for this feature branch to be reviewed and merged into its parent branch. But what to do after this feature branch is merged into its parent.

# 1. you can make a new branch off the grandparent branch (e.g. master), and cherry pick all the changes you made from your working branch.
# 2. squash all commits of the feature branch into the first commit of your working branch and rebase onto master. This will keep the history clean.
# Notice: potentially master could have some changes made in the places where the feature branch has made changes, and this strategy will overwirte all the changes made by the master in these places. If this is the case, strategy 1. should be used.

Cherry Pick

# cherry pick commit 
$ git cherry-pick <commit>

# resolve conflicts and commit if any
$ git add --all
$ git commit -m "your msg"

For advanced cherry-pick refer to this docs for more details.

Tags

# list
$ git tag

# regex
$ git tag -l "v1.8.5*"

# create
$ git tag <tagName>
$ git push <remoteAlias> <tagName>

# create annotated
$ git tag -a v1.4 -m "my version 1.4"

# create and attach to a specific commit
$ git tag -a <tagName> <commitId>

# push a tag
$ git push <remoteAlias> <tagName>

# push all tags
$ git push <remoteAlias> --tags

# pull all tags
git fetch origin --tags
git fetch --all --tags --prune

# check a branch at a tag
$ git checkout -b <branchName> <tagName>

# reset to tag
$ git tag BACKUP
$ git reset --hard BACKUP

# delete local tag
$ git tag -d TAG

# delete remote tag
$ git push --delete origin TAG

Logs

# show commits, press 'q' to quit
$ git log 

# show commits one line
$ git log --oneline

# search
$ git log -S "search string"

# count commits
$ git log --oneline | wc -l

# commit graph
$ git log --oneline --graph

# count commits by committers
$ git shortlog -s
$ git shortlog -sne

# commits by an author
$ git log --author "Author Name"

# commits by author and search string
$ git log --grep "search string" --author "Author Name"

# commits by time
$ git log --after="2014-02-12T16:36:00-07:00"
$ git log --before="2014-02-12T16:36:00-07:00"
$ git log --until="2014-02-12"
$ git log --since="yesterday"
$ git log --since="1 month ago"
$ git log --since="2 weeks 3 days 2 hours 30 minutes 59 seconds ago"

Stashing

# stash changes
$ git stash
$ git stash save "message"

# list
$ git stash list

# show changes
$ git stash show -p

# apply
$ git stash apply
$ git stash apply stash@{stash_number}

# drop
$ git stash drop

# apply and drop
$ git stash pop
$ git stash pop stash@{stash_number}

# apply stash to a new branch
$ git stash branch
$ git stash branch <branch> <stash>

Git diff

# file diff
$ git diff <file>
$ git diff --cached <file>

# commit diff
$ git show <commit>

# show all changes in staged
$ git diff --cached

# list of files to be pushed
$ git diff --stat <remote/branch>
$ git diff --stat origin/master

# code diff of the files to be pushed
$ git diff <remote/branch>

# full file paths of the files that will change, run:
$ git diff --numstat <remote/branch>

# compare branches
$ git diff <branch_1> <branch_2>
$ gitk <branch_1> <branch_2>

Git grep

# search content in commits
$ git grep -n 'search string'

# return x number of lines before and after of the matches
$ git grep -n -C<x> 'search string'

# only "before"
$ git grep -n -B<x> 'search string'

# only "after"
$ git grep -n -A<x> 'search string'

Git blame

# show code changes line by line, with authors
$ git blame [file]
$ git blame [file] -l
$ git blame [file] -l > file_change_log.txt

Other

git ssh config file

C:\Program Files\Git\etc\ssh\ssh_config

About

A repository for commonly used git commands.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published