Skip to content

Instantly share code, notes, and snippets.

@patrickelectric
Last active November 7, 2018 20:16
Show Gist options
  • Save patrickelectric/8dfb24482582b6f6cf52a8ed37a62f93 to your computer and use it in GitHub Desktop.
Save patrickelectric/8dfb24482582b6f6cf52a8ed37a62f93 to your computer and use it in GitHub Desktop.
Mastering git and all necessary super powers in a TLDR version !
#!/bin/bash
# Create issue in github remote origin
# git issue "title" "body"
remote="$(git remote get-url origin)"
owner_repo="${remote#*com/}"
array=(${owner_repo//// })
owner="${array[0]}"
repo="${array[1]}"
echo "Repo: $owner/$repo"
title="$1"
body="$2"
echo -n "Login: "
read login
echo -n "Pass: "
read -s pass
echo "xxxxxxx"
#read -s pass
output=$(curl -sS -H "Content-Type: application/json" \
-i https://api.github.com/repos/$owner/$repo/issues \
-u $login:$pass \
-d '{"title":"'"$title"'", "body":"'"$body"'"}' \
| grep "html_url" | grep "issues")
output=${output//\"}
echo ${output//\,}
[alias]
# Avoid git git commands
git = !git
# Create a branch from a github remote, using the PR number and the remote name
# git pr remote_name pr_number branch_name extra_args
pr = "!f() { git fetch $1 pull/$2/head:$3 $4; }; f"
# git cloneorg org
cloneorg = "!f() { \
ORG_URL=\"https://api.github.com/orgs/$1/repos?per_page=200\"; \
ALL_REPOS=$(curl -s ${ORG_URL} | grep html_url | awk 'NR%2 == 0' | cut -d ':' -f 2-3 | tr -d '\",'); \
for ORG_REPO in ${ALL_REPOS}; do git clone ${ORG_REPO}.git; done; \
}; f"
# List branchs by age
branch-by-age = for-each-ref --sort=committerdate refs/heads/ --format='%(authordate) \t %(refname:short)''
# git fix-old number
fix-old = "!f() { git commit --fixup=$(git rev-parse HEAD~$(($1-1))); }; f"
# Show ignored files
git status --ignored

# Show help
git help command

# Grep only tracked files
git grep -e "pattern"
# Grep only tracked files with extension
git grep -e "pattern" -- **/*.cpp
# Grep in staging are
git grep --cached "pattern"
# Find cursed trailing spaces !
git grep ' $' -- \*.h \*.cpp \*.qml
# Grep all tracked files that have ";"in the end of the line
# and return that does not have a space after the n in all cpp files !
git grep -e ";$" --and -e "return" --and --not -e "n " -- "**/*.cpp" # ULTIMATE POWER
# Search for something in all commits
git grep "something" $(git rev-list --all)

# Show all tracked files
git ls-files

# Git fetch all remotes
git fetch --all

# Show diff from HEAD and stash
git stash show -p stash@{0}
git diff stash@{0} HEAD
# Shor diff of line between hashs
git diff HEAD master -- myfile.md
# Create a patch from the n commits from the hash
git format-patch -n hash --stdout > magic_feature.patch

# Add in interactive
git add -p
# Add path from root folder (: = project root folder)
git add :/path

# Split an old commit in two
# 1. Start an interactive rebase
git rebase -i HEAD~4
# 2. Change one commit from 'pick' to 'edit' or 'e'
# Close and save the file
# 3. Clean the commit to create a new one
git reset HEAD~
# 4. Add things to the first commit
git add -p
git commit -sm "nice"
# 5. Add things to the second commit
git add -p
git commit -sm "feature"
# 6. Finish the rebase
git rebase --continue
# Now you have two commits !

# Commit a message with a sign
git commit -sm ""
# Merge the last uncommited added files
git commit --amend 

# Checkout and create branch
git checkout -b branch
# Checkout some parts of the code in interactive format
git checkout -p
# Go back last branch
git checkout -
# Checkout file to vertion in hash
git checkout c5f567 -- file

# List last branchs
git branch --sort=committerdate
$ Or the long version 
git for-each-ref --sort='-authordate:iso8601' --format=' %(authordate:relative)%09%(refname:short)' refs/heads

# Get a pull request
git fetch origin pull/PR_NUMBER/head:LOCAL_BRANCH_NAME

# Stash in interactive
git stash -p
# Stash untracked files
git stash save -u
git stash --include-untracked

# Rebase in interactive mode the last 3 commits
git rebase -i HEAD~3 

# Old style tig 
git log --graph
# Show only the last 3 logs
git log -3
# Show only commits that have pattern
git log --grep "patern"
# Show all commits that change a file
git log -- file
## Show all commits with diffs !
git log -p -- file
## Show commits between two points (changelog)
git log --oneline HEAD ^OLD_HASH
## Show commits between two points (changelog) with format
git log --oneline HEAD ^OLD_HASH --format=%s
## Show history of lines in file
git log -L start_line,+lines:path_file

# Show list of files that was modified in this branch compared to master
git diff-tree --no-commit-id --name-only -r HEAD master
## It's possible to interact with the files with for
# for x in $(git diff-tree --no-commit-id --name-only -r HEAD origin/master);do sed -i -e 's/-2016/-2018/g' $x; done;


# Show remotes information
git remote -v
# Delte remote
git remote delete remote_name

# Run git bisect with script
git bisect run ./test-error.sh
# Run git bisect with command
git bisect run make
# Run git bisect with inline commands
git bisect run sh -c "rm -rf build && mkdir build && cd build && cmake .. && make -j5"
# Note: Git bisect have a special exit code of 125, where should be used when no test can be done.

# Track remote branch
git checkout remote/branch --track

# Push all tags with all tracked branchs !
git push remote --mirror

# Create a mirror from a repository
git clone --bare repository
cd repository
git remote add mirror link
git push mirror --mirror

# Create a hg mirror
git clone hg::https://username@mail.com:pass@bitbucket.org/user/repo

# Cherry-pick range of commits
git cherry-pick branch~4..branch

# Push a tag or a branch with same name
git push remote refs/heads/branch_name
git push remote refs/tags/tag_name

# Util bash examples
## Add each individual modified file and commit it with name: message
for file in $(git ls-files --modified); do git add $file; git commit -sm "${$(basename $file)%.*}: message"; done
## Update banch1 and branch2 over origin/master and update my_remote
for branch in branch1 branch2; do git rebase origin/master $branch; git push my_remote $branch --force; done


# .gitignore
*.[oa] # Will ignore *.o and *.a
!exception # Ignore files except this one
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment