Skip to content
Bash

Git Basics

Basic Git operations.

By EZ4Code Team
gitbasic

Code

#!/bin/bash

# Initialize repository
git init
git init my-project  # Initialize in new directory

# Clone repository
git clone https://github.com/user/repo.git
git clone --depth 1 https://github.com/user/repo.git  # Shallow clone

# Config
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global core.editor vim
git config --global init.defaultBranch main

# View status
git status
git status -s  # Short format

# Add and commit
git add file.txt
git add .  # Add all changes
git add -p  # Interactive add
git commit -m "commit message"
git commit -am "message"  # Add tracked files and commit
git commit --amend  # Amend last commit

# View history
git log
git log --oneline --graph
git log --author="Alice"
git log --since="2 weeks ago"
git log -p file.txt  # Show file changes
git log --stat  # Show stats

# View diff
git diff              # Working dir vs staging
git diff --staged     # Staging vs HEAD
git diff HEAD         # Working dir vs HEAD
git diff branch1..branch2

# Undo operations
git checkout -- file.txt  # Undo working dir changes
git restore file.txt      # (new command) undo changes
git reset HEAD file.txt   # Unstage
git reset --soft HEAD~1   # Undo commit, keep changes
git reset --hard HEAD~1   # Undo commit, discard changes

# Remote repository
git remote add origin https://github.com/user/repo.git
git remote -v
git push -u origin main
git pull
git fetch

Explanation

Git basics include init/clone/config/add/commit/log/diff/restore/remote operations.

More Bash Snippets