Git in Bash
Version control commands for the command line
🔀 What is Git in Bash?
Git is a version control system that tracks changes in your code. Using Git commands in Bash allows you to manage repositories, commit changes, and collaborate with others directly from the terminal.
# Check Git version
git --version
# Initialize a new repository
git init
Output:
git version 2.39.0 Initialized empty Git repository in /home/user/project/.git/
Essential Git Commands
Initialize
Start a new Git repository
git init
Clone
Copy a repository to your machine
git clone <url>
Commit
Save changes with a message
git commit -m "message"
Push
Upload commits to remote server
git push origin main
🔹 Basic Git Workflow
The standard workflow involves checking status, staging files, committing changes, and pushing to a remote repository. This sequence is used daily by developers to track and share their work with team members.
# Check repository status
git status
# Add files to staging area
git add filename.txt
git add . # Add all files
# Commit changes
git commit -m "Add new feature"
# Push to remote repository
git push origin main
Output:
On branch main Changes to be committed: modified: filename.txt [main 3a2b1c4] Add new feature 1 file changed, 5 insertions(+)
🔹 Branching and Merging
Branches let you work on features independently without affecting the main codebase. You can create branches for new features, switch between them, merge completed work, and delete old branches when done.
# Create a new branch
git branch feature-login
# Switch to the branch
git checkout feature-login
# Create and switch in one command
git checkout -b feature-signup
# List all branches
git branch
# Merge branch into main
git checkout main
git merge feature-login
# Delete a branch
git branch -d feature-login
Output:
Switched to a new branch 'feature-signup' * feature-signup main Updating 3a2b1c4..7d8e9f0 Fast-forward
🔹 Pulling and Fetching
Keep your local repository synchronized with remote changes using pull and fetch commands. Pull downloads and merges changes automatically, while fetch only downloads them for review before merging manually.
# Fetch changes from remote
git fetch origin
# Pull changes and merge
git pull origin main
# Pull with rebase
git pull --rebase origin main
Output:
remote: Counting objects: 5, done. Updating 3a2b1c4..7d8e9f0 Fast-forward README.md | 2 ++ 1 file changed, 2 insertions(+)
🔹 Viewing History
Git maintains a complete history of all commits in your project. You can view commit logs with various formatting options, see specific file changes, and track who made what modifications over time.
# View commit history
git log
# Compact one-line format
git log --oneline
# Show last 5 commits
git log -5
# View changes in a commit
git show <commit-hash>
# View file history
git log --follow filename.txt
Output:
7d8e9f0 Add login feature 3a2b1c4 Update README 1f2e3d4 Initial commit
🔹 Undoing Changes
Git provides multiple ways to undo mistakes depending on whether changes are staged, committed, or pushed. You can discard local modifications, unstage files, amend commits, or revert to previous states safely.
# Discard changes in working directory
git checkout -- filename.txt
# Unstage a file
git reset HEAD filename.txt
# Undo last commit (keep changes)
git reset --soft HEAD~1
# Undo last commit (discard changes)
git reset --hard HEAD~1
# Revert a commit (creates new commit)
git revert <commit-hash>
Output:
Unstaged changes after reset: M filename.txt HEAD is now at 3a2b1c4 Update README
🔹 Remote Repositories
Remote repositories are versions of your project hosted on servers like GitHub or GitLab. You can add multiple remotes, view their URLs, rename them, and remove connections when no longer needed.
# View remote repositories
git remote -v
# Add a remote repository
git remote add origin https://github.com/user/repo.git
# Change remote URL
git remote set-url origin <new-url>
# Remove a remote
git remote remove origin
# Rename a remote
git remote rename origin upstream
Output:
origin https://github.com/user/repo.git (fetch) origin https://github.com/user/repo.git (push)