Git – Versioning System

Git – Versioning System

Git is one of the most fundamental and essential tool in computer science today. It is certainly the most required tool for not only every software engineer but also for the authors and those who want to keep track of their work in terms of increments, work collaboratively on projects and progress incrementally.

Git is a distrbuted versioning system. That means, every user is responsible for keeping a copy of the code base/resource. Below are the essential commands that will be helpful as a checklist.

Configuration

# Configure your name
git config --global user.name "Your Name"

# Configure your email
git config --global user.email "your@email.com"

# Check your configuration
git config --list

Basic commands

# Initialise the repository - New Repository
git init 

# Clone the repository
git clone -b <branch-name> <repo-url> <folder-name>

# Check status of files
git status

# Add files
git add <path>

# Remove file
git rm <path>

Commits

# Commit
git commit -m "Meaningful Message"

# Amend commit (Amend by adding files)
git commit --amend --no-edit

# Amend commit message
git commit --amend -m "New message"

# Undo last commit (Keep changes)
git reset --soft HEAD~1

# Undo commit and unstage changes
git reset --mixed HEAD~1

# Remove commit and changes completely
git reset --hard HEAD~1

Common types

  • feat: → new feature
  • fix: → bug fix
  • docs: → documentation
  • refactor: → code improvement
  • test: → tests
  • chore: → maintenance

Tags

# Create tag
git tag <tag>

# Create tag with message
git tag -a <tag> -m "Message"

# Tag a specific commit
git tag -a v1.0.0 <commit-hash> -m "Release"

# List tags
git tag

# Push tags
git push origin --tags

# Delete tags
git tag -d <tag>
git push --delete origin tag <tag>

Branches

# Create a branch
git checkout -b <branch-name>

# Checkout a branch
git checkout <remote-url> <branch-name>

# Push a branch
git push <remote-url> <branch-name>

# Delete a branch on remote
git push --delete <remote-url> <branch-name>

# Delete a branch locally
git branch -d <branch-name>

# Merge a branch 
git merge <destination-branch>

# Rebase branch
git rebase <base-branch-name>

Stash

# List the stashes
git stash list

# Show the name of files in Stash number 0
git stash show --name-only

# List the names of files in Stash number 2
git stash show --name-only stash@{2}

# Show full patch
git stash show -p stash@{0}

# Drop the stash
git stash drop

Git Worktree

Allows you to having multiple working directories linked to the same repository, enabling you to checkout out multiple branches simultanously without repeated branch switching or cloning. This is ideal for parallel development, hotfixes and isolated testing.

# Listing active worktrees
git worktree list

Git Submodules

When you need to use another project from within it, when the sub-project is a git repository in itself, submodules is the way to manage. In other words, it serves as a soft link, just as an analogy to Linux filesystem softlinks.

# Add a submodule
git submodule add <git_url> <directory>