Logo
← Back to Blog

Development and optimization

Git Notes

A personal reference for the Git commands I use most often, from repository setup and everyday commits to branches, tags, submodules, Git LFS, history inspection, and recovery commands.

These are personal working notes collected over time. The page is organized as a practical command reference rather than a complete Git tutorial.

Repository setup

Clone an existing repository

Terminal
git clone <repo-url>

Create a repository from a local folder

Create a folder, place the code inside it, and initialize Git.

Terminal
git init

Add the remote repository.

Terminal
git remote add <shortname> <repo-url>

# <shortname> is the name you want to give the remote
Note: The remote name is commonly origin, but Git allows another short name.

Configure Git identity

Terminal
git config --global user.name "FIRST_NAME LAST_NAME"
git config --global user.email "MY_NAME@example.com"

Inspect or change a remote

Terminal
git remote -v

git remote set-url origin <newGit>

git remote rm <remote_name>

Add, commit, and push

Basic workflow

Terminal
git add --all
git commit -m "contents wanting to note"
git push origin <branch_name>

Undo Git add

Terminal
git restore --staged <file>
Note: This removes the file from the staging area while keeping the local file and its changes.

History and differences

See commit history

Terminal
git log
git log --pretty=oneline

git diff

Use git diff to inspect differences between the current working state and commits, or between two commits.

Terminal
git diff
git diff <commitName>
git diff <commitName1> <commitName2>
git diff <filename>

git show

In these notes, git show is used to inspect the changes introduced by a specific commit.

Terminal
git show <commitName>
git show <commitName1> <commitName2>
git show <commitName1> <commitName2> --color-words
Terminal output from git show with word level color highlighting
Example of comparing commit changes with word level color highlighting.

Undo and reset

Use destructive commands carefully.

Commands using --hard, --force, or history rewriting can remove local changes or change remote history.

Undo a commit before pushing

Terminal
git reset HEAD~1

Reset to a previous point

Terminal
git reset --hard HEAD
git reset --hard HEAD^
git reset --hard <commit_name>
git push origin HEAD --force

Keep current code while removing later commits

The original notes use a soft reset when the goal is to move the branch pointer while preserving the current work for recommitting.

Terminal
git reset --soft <commit>
Note: The commits after the selected point are removed from the branch history, while the current changes remain available for recommitting.

Remove untracked files and directories

Terminal
git clean -fd
# -f: force
# -d: remove directories
Note: This command removes untracked files and directories, so check the repository state before running it.

Tags

Create and push a tag

Terminal
git tag -a <tagName> -m "message"
git push origin <tagName>
git push --tags

Tag a particular commit

Terminal
git log --pretty=oneline
git tag -a <tagName> <commit> -m "message"
git push origin <tagName>

Inspect, delete, or rename tags

Terminal
git tag -n
git tag
git tag -d <tag_name>
git ls-remote --tag
git push origin --delete <tag_name>
git tag <new_tag> <old_tag>
git push --tags

Branches and stash

Create and switch branches

Terminal
git branch <name> <commit>
git checkout <branch>
git push origin <branch_name>

Delete a branch

Terminal
git branch -d <branch_name>
git push origin --delete <branch_name>

Merge

Check out the branch that should receive the changes, then merge the source branch into it.

Terminal
git merge <branch_to_merge_from>

Stash temporary work

Terminal
git stash push -m <message>
git stash list
git stash pop --index <index>
git stash apply --index <index>
git stash clear

Submodules

Add a submodule

Terminal
git submodule add <url_to_repo> <projectfolder>

Initialize submodules after cloning

Terminal
git submodule update --init --recursive
Note: Use this after cloning when the repository contains nested submodules that were not initialized automatically.

Clone together with submodules

Terminal
git clone --recurse-submodules <repo-url>

Git LFS

Initialize and track files

Terminal
git lfs install
git lfs track "*.psd"
git add .gitattributes

Migrate existing Git history to LFS

Terminal
# Dry run
git lfs migrate info --everything --include="*.zip"

# Migrate history
git lfs migrate import --everything --include="*.zip" --verbose

# Current history scope only
git lfs migrate import --include="*.zip" --verbose

Pull missing LFS files

Terminal
git lfs pull
Note: Use this when the repository is present but the large files managed by Git LFS are missing locally.

Files and .gitignore

Remove a tracked file

Terminal
git rm --cached file1.txt
git commit -m "remove file1.txt"

git rm file1.txt
git commit -m "remove file1.txt"

Create a .gitignore file

Terminal
touch .gitignore

Example patterns from the original notes:

.gitignore
*.exe
*.rar
*.zip

venv_*
**/.vscode/
Note: Each line is a pattern. The examples ignore selected archive files, virtual environment folders, and nested VS Code folders.

Refresh tracking after changing .gitignore

Terminal
git rm -r --cached .
git add .
git commit -m "Update .gitignore"

Proxy and other commands

Proxy configuration

Terminal
# Check current proxy configuration
git config --global --get-regexp http.*

# Configure proxy
git config --global http.proxy http://proxyUsername:proxyPassword@proxy.server.com:port

# Remove proxy
git config --global --unset http.proxy

Repository size and cleanup

Terminal
git count-objects -vH
git gc --prune=now --aggressive

Quick reference

Clonegit clone <repo-url>
Statusgit status
Historygit log --pretty=oneline
Remotesgit remote -v
Current changesgit diff
Commit detailsgit show <commit>