Skip to content

Latest commit

 

History

History

git

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Git

Keywords: Source Code Management (SCM), Codebase, Version Control System (VCS)

References

Alternatives

CLI

Installation

# Using Homebrew
brew install git

# Using APT
sudo apt update
sudo apt -y install git

# Using YUM
yum check-update
sudo yum -y install git

# Using APK
sudo apk update
sudo apk add git

# Using Zypper
sudo zypper refresh
sudo zypper install -y git-core

# Using Chocolatey
choco install -y git

Usage

# Initialize
git init
# or, define branch
git init --initial-branch='develop'

# Create
git tag <version>

# Describe
git tag -a <version> -m <message>

# Push with Tags
git push origin master --tags

# List
git tag -l

# Checkout
git checkout tags/<version>

# Remote
git ls-remote --tags

# Delete
git tag -d <version>

# Remote
git push --delete origin <version>

# Update Index
git update-index --assume-unchanged <file>
git update-index --no-assume-unchanged <file>

# Prevent to receive changed files
git update-index --no-assume-unchanged <file>

# Ignore local changed files
git update-index --assume-unchanged <file>

# List files with `assume-unchanged`
git ls-files -v | grep '^h'

Tips

Add Git Keep

/cache/*
!/cache/.gitkeep
/logs/*
!logs/.gitkeep

Remove Local Branches Except Main

git branch --no-color | \
  grep -v 'master\|stable\|main' | \
    xargs git branch -d

Remove Merged Branches

git branch --merged | \
  egrep -v '(^\*|main)' | \
    xargs git branch -d

Tags

#
git tag -am 'Version 1.0.0' v1.0.0 master
git tag -am 'Version 1' v1 master

#
git tag -am 'Version 1.0.1' v1.0.1 master
git tag -am 'Version 1' --force v1 v1.0.1

#
git push --follow-tags

Merge Strategies

  • Fast Forward Merge

    • No new commits on the base branch
    • No merge commit on the base branch when merged
    • Provides a linear history
    git rebase
  • Recursive Merge

    • New commits on the base branch
    • A merge commit is created on the base branch
    • Merge commit has 2 parents with a true diverged history
    git merge --no-ff

Command-line completion

# Using Antigen
antigen bundle git

Remote

#
git remote -v

#
git remote remove origin <url>
git remote add origin <url>

# or, using set-url
git remote set-url origin <url>

Create a New Branch

# Creating a branch from a commit
git branch <branch-name> <hash>

# Creating a branch from a tag
git branch <branch-name> <tag-name>

Visual Studio Code

#
code --install-extension mhutchie.git-graph

#
jq '."recommendations" += ["mhutchie.git-graph"]' "$HOME"/.vscode/extensions.json | sponge "$HOME"/.vscode/extensions.json
Search Exclusion
# Globally
jq '."search.exclude"."**/.git" |= true' "$HOME"/.vscode/settings.json | \
  sponge "$HOME"/.vscode/settings.json

# Locally
jq '."search.exclude"."**/.git" |= true' "$PWD"/.vscode/settings.json | \
  sponge "$PWD"/.vscode/settings.json
Files Exclusion
# Globally
jq '."files.exclude"."**/.git" |= false' "$HOME"/.vscode/settings.json | \
  sponge "$HOME"/.vscode/settings.json

# Locally
jq '."files.exclude"."**/.git" |= false' "$PWD"/.vscode/settings.json | \
  sponge "$PWD"/.vscode/settings.json

Most recent changed branch

git for-each-ref --sort=committerdate refs/heads/ --format='%(committerdate:short) %(refname:short)'

Show latest committer

git for-each-ref \
  --sort=-committerdate refs/heads/ \
  --format='%(committerdate:short) %(authorname) %(refname:short)'

Command-line completion

# Using Oh My Zsh
sed -ri 's/^plugins=\((.*)\)/plugins=\(\1 git\)/g' ~/.zshrc

source ~/.zshrc

rm ~/.zcompdump*

Version

# Current
git describe --tags

#
git describe --tags --abbrev=0

#
git describe --tags | sed 's/^v//'

#
git describe --tags | cut -d - -f 1 | sed 's/^v//'

Developer Certificate of Origin (DCO)

# Commit
git commit -sm '<message>'

# Ammend
git commit --amend --signoff

# Push
git push --force-with-lease

Clean GitHub Pages

#
git clone --no-checkout <repo>

#
git checkout --orphan gh-pages

#
git clean -fdx

#
git push origin gh-pages

Issues

Taking Long Time

# Try disable Hooks
git config --unset core.hooksPath

Credentials

Missing or invalid credentials.

TODO

TBD

error: cannot lock ref 'refs/remotes/origin/Feat/HAB-337': unable to resolve reference 'refs/remotes/origin/Feat/HAB-337'

TODO

Missing Logs Directory

error: cannot update the ref 'refs/remotes/origin/Feat/HAB-357': unable to create directory for '.git/logs/refs/remotes/origin/Feat/HAB-357': No such file or directory
mkdir ./.git/logs/refs/remotes/origin/Feat/HAB-357

Index Lock

Git: fatal: Unable to create '/path/to/project/.git/index.lock': File exists.
rm ./.git/index.lock

Broken Reference

error: cannot lock ref 'refs/remotes/origin/fix/dbml': 'refs/remotes/origin/fix' exists; cannot create 'refs/remotes/origin/fix/dbml'
git remote prune origin

Allow Unrelated Histories

fatal: refusing to merge unrelated histories
git pull origin master --allow-unrelated-histories

Certificate

SSL certificate problem: unable to get local issuer certificate
# Local
git config http.sslVerify false

# Or, global
git config --global http.sslVerify false

# Or, environment
GIT_SSL_NO_VERIFY=true git clone <repo>

Received HTTP code 502 from proxy after CONNECT

Received HTTP code 502 from proxy after CONNECT
git config --unset http.sslVerify

RPC failed

error: RPC failed; result=22, HTTP code = 411
git config http.postBuffer 524288000

Editor Issue

Output message after run git commit --amend:

error: Terminal is dumb, but EDITOR unset
Please supply the message using either -m or -F option.

Solve this issue setting the core.editor value:

git config --global core.editor <value>

Use vim, atom --wait, subl -n -w and mate -w.

export GIT_EDITOR=vim

Push

error: src refspec project does not match any.
error: failed to push some refs to 'project@xyz.tld:project'
git push -u origin master