Skip to content

Record git commands which are frequently used.

Notifications You must be signed in to change notification settings

eyvindove/git-tips

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 

Repository files navigation

Git Tips

This repository is used to record some git commands which are frequently used.

Git Document Reference Manual

If you like this tips, don't forget to give a star! 🌟


Table of Contents

Setup and Config

Configuration

Set and get git global or repository settings

# Set global user settings
$ git config --global user.name xxx
$ git config --global user.email [email protected]

# Set repository user settings
$ git config user.name xxx
$ git config user.email [email protected]

# Remove settings
$ git config --unset user.name

# Get settings
$ git config --global --list
$ git config --list

# Use alias
$ git config --global alias.br branch

# Avoid line ending problem under cross platform development
$ git config --global core.autocrlf true

Useful Alias

alias.br=branch
alias.chp=cherry-pick
alias.fp=fetch --prune
alias.cm=commit
alias.co=checkout
alias.st=status
alias.lg=log --oneline --graph
alias.la=log --graph --all --pretty=format:"%C(auto)%h -%d %s%Creset %Cblue(%cr)%Creset %C(dim white)<%an>%Creset"

Getting and Creating Projects

Initialization

Create an empty git repository or reinitialize an existing one

$ git init

Clone

Clone a repository into a new directory

$ git clone <directory>

Basic Snap Shotting

Status

Show the working tree status

$ git status

Add

Add file contents to the index

$ git add <file_path>

# Add all un-staged files in the index
$ git add .

Commit

Record changes to repository

$ git commit

$ git commit -m <commit_message>

# Add and commit all unstaged files with commit message
$ git commit -am <commit_message>

Diff

Show changes between commits, commit and working tree, etc.

$ git diff <file_path>

$ git diff HEAD~1

$ git diff <commit-1> <commit-2>

# Compare the difference between index changes and last commit
$ git diff --cached <file_path>

Restore

Restore working tree files

$ git restore <file_path>

# Restore staged files
$ git restore --staged <file_path>

Reset

Reset current HEAD to specified state

$ git reset [--soft | --mixed | --hard] <commit>

Branching and Merging

Branch

List, create, or delete branches

# List all branches in your repo, and which branch you're currently in
$ git branch

# Create a branch
$ git branch <branch_name>

# Delete the branch
$ git branch -d <branch_name>

# Delete the branch (FORCE)
$ git branch -D <branch_name>

# Change the branch name
$ git branch -m <older_branch_name> <new_branch_name>

Checkout

Switch branches or restore working tree files

# Switch to another branch
$ git checkout <branch_name>

# Create a new branch & switch to it
$ git checkout -b <branch_name>

# Cancel the changed file
$ git checkout -- <file>

Merge

Join two or more histories together

$ git merge <branch>

# Merge without fast-forward
$ git merge --no-ff <branch>

Log

Show commit logs

$ git log

# Specify date
$ git log --before=<date> --after=<date>

# Specify author
$ git log --author=<author_name>

Stash

Stash the changes in a dirty working directory away

$ git stash

# List out current stash
$ git stash list

# Push the files wanna stash
$ git stash push

# Output the stashed file
$ git stash apply

# Output specific stashed file
$ git stash apply <stash>

# Remove a single stashed state & apply it on top of current working tree state
$ git stash pop

# Remove a single stash entry from the list of stash entries
$ git stash drop <stash>

Tag

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

$ git tag <tag_name>

# Delete a tag
$ git tag -d <tag_name>

# Delete all local tags
$ git tag -l | xargs git tag -d

Sharing and Updating Projects

Fetch

Download objects and refs from another repository

$ git fetch

# Fetch all remote
$ git fetch --all

# Before fetching, remove any remote-tracking references that no longer exist on the remote
$ git fetch --prune
# Fetch & rebase branch without checkout
$ git fetch origin <remote_branch>:<local_branch>

Push

Update remote refs along with associated objects

$ git push

# Add upstream reference
$ git push [-u | --set-upstream] <remote_name> <repository_url>

Remote

Manage set of tracked repositories

# Show remote url after name
$ git remote -v

# Add remote
$ git remote add <remote_name></remote_name> <repository_url>

# Remove remote
$ git remote remove <remote_name> <repository_url>

# Change remote URL
$ git remote set-url <remote_name> <repository_url>

Submodules

# Add a submodules
$ git submodule add <repository> <path>

# Occasionally update the submodule to a new version
$ git -C <path> checkout <new-version>
$ git add <path>
$ git commit -m "update submodule to new version"

# Check submodules status
$ git submodule status

# Cloning or pulling a repository containing submodules
$ git submodule init
$ git submodule update
# Shorthand
$ git submodule update --init --recursive

# Remove submodules
$ git rm <path>

Patching

Cherry-Pick

Apply the changes introduced by some existing commits

$ git cherry-pick <commit_hash_1> <commit_hash_2> ...

# Pick without commit
$ git cherry-pick <commit_hash_1> --no-commit

Rebase

Reapply commits on top of another base tip

$ git rebase [-i | --interactive] <new_base>

Revert

Revert some existing commits

$ git revert

Debugging

Grep

Print lines matching a pattern

# Search with line number
$ git grep -n <search_text>

Administration

Reflog

Manage & track reflog information

$ git reflog

Gitk

Open a Git repository browser

$ gitk

# Show all references (branches, tags, etc.)
$ gitk --all

About

Record git commands which are frequently used.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published