Post

Use Git

Examples of Git/GitHub Workflows

Use Git

Git Basics

You need to know some basic terminal commands to navigate the file system and interact with Git repositories.

Go to Use Terminal.

Initialize and check status

1
2
3
4
5
6
7
8
9
10
11
# Start a new Git repository in your current folder
git init

# Check current status: staged/unstaged files, untracked files, etc.
git status

# See your current branch (usually "main" after setup)
git branch

# See if this repo is connected to any remote (e.g., GitHub)
git remote -v
1
2
3
4
5
6
7
8
# Link your local repo to a GitHub repository
git remote add origin https://github.com/your-username/your-repo.git

# Optional: Rename the current branch to "main" (standard name)
git branch -M main

# Push code for the first time and set the "origin/main" as default upstream
git push -u origin main
1
2
3
4
5
6
7
8
9
10
11
# Check the current linked remote repository
git remote -v

# Change the remote URL if the repo name or account changes
git remote set-url origin https://github.com/your-username/new-repo.git

# Pull latest changes from GitHub before pushing your own
git pull origin main

# Push your changes to GitHub
git push origin main

Stage, commit and push (sync) changes

Repeat these 3 steps every time you make changes locally that you want to save to GitHub.

1
2
3
4
5
6
7
8
# Add all current changes to staging (ready to be committed)
git add .

# Save a snapshot of changes with a message
git commit -m "Your commit message"

# Push commits to GitHub
git push origin main

Tags

Tags are like bookmarks in your code history. Great for version releases.

1
2
3
4
5
6
7
8
# Create an annotated tag (useful for versioning)
git tag -a v1.0 -m "Version 1.0 - Initial stable release"

# Push the tag to GitHub
git push origin v1.0

# Check out a specific tag (detached HEAD state)
git checkout v1.0
1
2
3
4
5
6
7
8
9
# Delete a tag locally if you made a mistake
git tag -d v1.0

# Delete the same tag from GitHub
git push origin --delete v1.0

# Recreate and push the updated tag again
git tag -a v1.0 -m "Updated version v1.0"
git push origin v1.0

Branches

Use branches to work on new features or fixes without affecting your main code until itโ€™s ready.

1
2
3
4
5
6
7
8
9
# Create and switch to a new feature branch
git checkout -b feature/my-new-feature

# Stage and commit your changes
git add .
git commit -m "Add: new feature"

# Push the new branch to GitHub
git push origin feature/my-new-feature
1
2
3
4
5
6
7
8
9
10
11
# Switch back to main branch
git checkout main

# Merge the feature branch into main
git merge feature/my-new-feature

# Push the updated main branch
git push origin main

# Delete the local branch (cleanup)
git branch -d feature/my-new-feature
1
2
3
4
5
6
7
8
# Delete a branch from GitHub
git push origin --delete feature/my-new-feature

# Remove references to deleted remote branches (clean up)
git fetch --prune

# View all branches on the remote
git branch -r

git stash โ€“ Save Changes Temporarily Without Committing

Use this when youโ€™re in the middle of something but need to quickly switch branches or pull changes without committing messy code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Save your uncommitted changes (stash them away)
git stash

# See a list of stashed changes
git stash list

# Reapply the most recent stash and remove it from the list
git stash pop

# Reapply without deleting the stash (good for testing)
git stash apply

# Delete all stashes
git stash clear

git diff โ€“ See Whatโ€™s Changed

Use git diff to see changes between commits, branches, or your working directory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# See changes in your working directory compared to the last commit
git diff

# See changes between the last commit and the staging area
git diff --cached

# See changes between two commits
git diff <commit1> <commit2>

# See changes between your current branch and another branch
git diff main..feature/my-new-feature
git diff branch-a..branch-b

# See changes in a specific file
git diff path/to/file

git log โ€“ View Commit History

Use this to review what was changed, by whom, and when.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# See full commit history (most recent at top)
git log
git log --oneline

# View detailed commit history with branches
git log --oneline --graph --all

# View changes made in a specific commit
git show <commit-hash>

# View changes made to a specific file
git log -- path/to/file

# View who changed specific lines in a file
git blame path/to/file

Undoing Changes

If you make a mistake, Git provides several ways to undo changes.

reset --hard will permanently delete uncommitted changes. Use only when you are absolutely sure. revert is preferred in shared projects because it doesnโ€™t change history โ€“ it just adds a โ€œreverseโ€ commit.

1
2
3
4
5
6
7
8
9
10
11
# Unstage files (but keep changes)
git reset

# Undo last commit, keep changes unstaged
git reset --soft HEAD~1

# Undo last commit and discard changes
git reset --hard HEAD~1

# Undo a commit by creating a new one that reverses it
git revert <commit-hash>

Real-world Examples

Typical Workflow

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Every morning:
git checkout main
git pull origin main      # Always sync first

# Create a new task branch
git checkout -b feature/user-profile

# --- Code, code, code... ---

# Save your progress
git add .
git commit -m "Build user profile page"
git push origin feature/user-profile

# When done, create a Pull Request on GitHub
This post is licensed under CC BY 4.0 by the author.