Skip to content

Instantly share code, notes, and snippets.

@tinhtun
Last active October 24, 2018 01:37
Show Gist options
  • Select an option

  • Save tinhtun/0f39cd47c2637efa24dfe5af5a6928d7 to your computer and use it in GitHub Desktop.

Select an option

Save tinhtun/0f39cd47c2637efa24dfe5af5a6928d7 to your computer and use it in GitHub Desktop.
#tinhtun #git

Git Commands

Photo from Google

Configuration

$ git config --global user.name "John Doe"
$ git config --global user.email "john@example.com"
$ git config --global --list

Add Git aliases in global configuration file C:\Users\<user_id>\.gitconfig

[alias]
    logg = log --oneline --graph --decorate
    alias = config --get-regexp '^alias\\.'
    state = !git fetch origin && git remote show origin && :
    sync = !git fetch origin && git remote prune origin && :

Clone a remote repository

This creates a new directory with the name of the repository.

$ git clone https://github.com/user/repository.git

Clone a remote repository in the current directory

$ git clone https://github.com/user/repository.git .

Discard local changes

git reset --hard 
git clean -df
git status

# Discard interactively
$ git clean -d -i

Discard Last Local Commit

$ git reset --hard HEAD~1

Switch remote branch

git fetch origin <branch-name>
git checkout <branch-name>
git branch 

Stash

# You are in the middle of development and want to pull from remote?
# Hide your local modifications with "stash"
$ git stash
# OR
$ git stash save --include-untracked "Optional message"
# Now pull the changes from remote.
$ git pull
# Restore your local modifications and continue your works!
$ git stash pop

Resolving Conflict after Pull

# After you do "git pull", you probably might be in conflict state
# Configure merging tool if you haven't done before.
# Assuming that you have already installed TortoiseSVN
$ git config --global merge.tool tortoisemerge
# Invoke mergetool and it will bring you all the conflicted files
$ git mergetool

Pulling Changes from Release Branch into Feature Branch

# Pull the changes from remote release branch into your local feature branch
# Assuming that you are in branch feature/xxxx
$ git pull origin release/xxxx
# Push the merging result to your remote feature branch
$ git push

Create and Apply Patch

# Create patch from a commit ID
$ git format-patch <COMMIT_ID>
# Apply the patch to current branch. Append signoff by current user info to the commit message.
$ git am --signoff < patch_file.patch

Filtering logs

# Git log filter by author without merge commits
$ git log --author="Tin Htun" --no-merges
# Display in one line format
$ git log --pretty=tformat:"%H [%an] %s" --author="Tin Htun" --no-merges

Filter commits and get the list of file changes

# filter Git commits by JIRA ticket for example, then get the included file list and remove duplicates
$ git log --name-only --pretty=format: --grep='filter text' --no-merges | sort | uniq
# for multiple JIRA ticket
$ git log --name-only --pretty=format: --grep='Ticket1|Ticket2' --extended-regexp --no-merges | sort | uniq
(OR)
# set the global config and you don't need to use --extended-regexp
$ git config --global grep.extendRegexp true

Reverting commit

# Revert the changes from commit(s), create new commit with the changes and default commit message
$ git revert --no-edit [commit id(s)]

Combining commits

# To squash the last 3 commits into one:
git reset --soft HEAD~3
git commit -m "New message for the combined commit"

Cherrypick

# pick commit from somewhere and apply on top of current HEAD
git cherry-pick <space separated commit ids>

TITLE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment