Anton Shuvalov

Git Basics

GIT Basics

Intro

In this article, I've compiled a comprehensive list of Git commands, drawn from my own extensive usage as a developer. These commands cover repository management, daily workflow tasks, and repository analysis.


How to Manage a Repo

# initialize a git repo in the dir:
git init 

# Add a remote URL:
git remote add origin git@bitbucket.org:company/api.git

# Delete a remote URL:
git remote rm origin

# List remotes:
git remote -v

# Clone a repo:
git clone git@bitbucket.org:company/api.git

# Clone a repo with a specific SSH id:
git clone git@bitbucket.org:company/api.git --config core.sshCommand="ssh -i ~/.ssh/specific_id_ed25519"

# Clone a repo with all submodules
git clone git@bitbucket.org:company/api.git --recursive

# Add a submodule into a given path
git submodule add git@bitbucket.org:company/api.git path

# Clone missed submodules
git submodule update

# Recursively remove all untracked files
git clean -fd

How to Do Daily Work

# List changed files
git status

# Add all
git add .

# Add a specific file
git add ./src/_app.tsx

# Write a commit message in $EDITOR, then commit
git commit

# Commit with a message
git commit -m "debug"

# Change last commit message
git commit --amend

# Rebase last 5 commits
git rebase -i HEAD~5

# Push `master` branch into an `origin` remote
git push origin master

# Push and overwrite the whole branch
git push -f origin master

# Pull changes from origin/master into a local branch
git pull origin master

# Fetch all remote updates
git fetch

# Show commit log
git log

# Consice human-readable commit log
git log --pretty=oneline --abbrev-commit

# Show commit log along with the diffs
git log --patch --stat

# Merge local branch into the current one
git merge

# Check diff for last commit
git diff HEAD~1

# Check diff between 2 commits
git diff 5d2ac6a..befc286

# Check diff between 2 files not in a git repo
git diff --no-index before.html after.html

# Make a patch from a diff
git diff --no-index before.html after.html > patch.diff

# Make a patch from commit
git format-patch -1 185d3fb

# Apply a patch
git apply patch.diff

# Copy commit into the current branch
git cherry-pick c5747e1

# Reset a file to match the specified branch
git restore -s origin/develop -- package-lock.json

# Remove file from git, but keep it locally
git rm --cached services/kafka

# Delete a branch
git branch -D origin/main

# Show a specific commit, tag or branch HEAD
git show 1.1.0

# Reset files to match the current branch HEAD
git reset

How to Configure Git

# Set default branch for `git init`
git config --global init.defaultBranch master

# Set global userame
git config --global user.name "Anton"

# Set repository-specific userame
git config --add user.name "Not Anton"

# Set global email
git config --global user.email "anton@company"

# Set repository-specific email
git config --add user.email "notanton@company"

# Set repository-specific SSH key
git config --add core.sshCommand "ssh -i ~/.ssh/repo_specific_id"ยท

How to Analyze a Repo

# Show contributors sorted by amount of commits
git shortlog -s | sort -k1,1nr

# Next ones are from git extras: https://github.com/tj/git-extras

# Show repo summary, such as repo age, contributors, commits per contributor, etc
git-summary

# Show most changed files
git-effort --above 30

# Generate AUTHORS
git-authors

# Generate CHANGELOG
git-changelog

Git Workflows



Created with obsidian-blog