Chapter 5 Essential Git Commands



Chapter introduction image
Image credit: Pexels



5.1 Understanding Git Workflow

Before diving into commands, let’s understand the basic Git workflow:

Basic Workflow:

  1. Modify files in your working directory
  2. Stage changes you want to include in next commit
  3. Commit staged changes to repository
  4. Push commits to GitHub

5.2 Essential Git Commands

5.2.1 1. Starting a Repository

Option A: Clone existing repository

git clone https://github.com/username/repository-name.git
cd repository-name

Option B: Initialize new repository

mkdir my-project
cd my-project
git init

5.2.2 2. Check Status

Most important command - use frequently:

git status

This shows:

  • Which files are modified
  • Which files are staged
  • Which files are untracked

5.2.3 3. Stage Changes

Stage specific files (rarely used):

git add filename.txt
git add folder/

Stage all changes (used almost always):

git add .

5.2.4 4. Commit Changes

Commit with message (used almost always):

git commit -m "Add introduction chapter"

Commit with detailed message (rarely used):

git commit -m "Add introduction chapter

- Added overview of research methodology
- Included literature review section
- Fixed formatting issues in references"

5.2.5 5. View History

See commit history:

git log

See compact history:

git log --oneline

See last 5 commits:

git log --oneline -5

5.2.6 6. Synchronize with GitHub

Push your changes to GitHub:

git push

Pull latest changes from GitHub:

git pull

5.2.7 7. Undo Changes

⚠️ WARNING: These commands are for experienced users only! If you’re new to Git, please consult a more experienced Git user before venturing into this jungle. These commands can permanently delete your work if used incorrectly.

Unstage files:

git reset filename.txt

Discard changes in working directory:

git checkout -- filename.txt

Undo last commit (keep changes):

git reset --soft HEAD~1

⚠️ Dangerous: Undo last commit and discard changes:

git reset --hard HEAD~1

5.3 Daily Research Workflow

# Start your day
git status                    # Check current state
git pull                     # Get latest changes

# Work on your files
# ... edit files ...

# Save your work
git add .                    # Stage all changes
git status                   # Review what you're committing
git commit -m "Update analysis results"
git push                     # Share your changes

Next Chapter Preview: Now that you know the essential Git commands, you’re ready to put everything together into a complete workflow. In the next chapter, we’ll create your first GitHub repository, connect it to your local computer, and learn the three-command workflow that handles 90% of your daily Git needs.