Technical Notes / Version Control
Git & GitHub Reference
Practical guidance for using Git locally and GitHub remotely, designed as a daily reference for real-world projects.
Core concepts
- Git: version control system running on your machine.
- GitHub: remote platform for hosting Git repositories.
- Repository: project folder tracked by Git.
- Commit: snapshot of changes.
- Branch: parallel line of development.
- Remote: external repository (usually called
origin).
Initial configuration (run once)
Set your identity for commits:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
Set default main branch:
git config --global init.defaultBranch main
Create or clone repositories
Initialize an existing project
git init
Creates a new Git repository in the current directory.
Clone from GitHub
git clone https://github.com/username/repository.git
Downloads a remote repository.
Status and history
git status
Shows modified files, staged files, and untracked files.
git log
Full commit history.
git log --oneline --graph --decorate
Compact history with branch visualization.
Staging and committing changes
Stage files
git add file.txt
Adds a file to the staging area.
git add .
Stages all changes.
Create a commit
git commit -m "Clear and meaningful message"
Saves staged changes into the repository history.
Tip: Keep commits small and messages descriptive.
Branches
git branch
Lists local branches.
git branch feature-name
Creates a new branch.
git checkout feature-name
Switches to another branch.
git checkout -b feature-name
Creates and switches to a new branch.
git merge feature-name
Merges a branch into the current one.
Extra: rename or delete branches
# Rename current branch
git branch -m new-name
# Delete local branch
git branch -d branch-name
# Delete remote branch
git push origin --delete branch-name
Remotes (GitHub)
git remote -v
Displays configured remotes.
git remote add origin https://github.com/username/repo.git
Links your local repo to GitHub.
Extra: change remote URL
git remote set-url origin https://github.com/username/new-repo.git
Push changes
git push
Uploads commits to the tracked remote branch.
git push origin main
Pushes the main branch.
git push -u origin feature-name
Pushes a new branch and sets upstream tracking.
Fetch vs Pull
git fetch
Downloads changes without merging.
git pull
Downloads and merges changes into the current branch.
Rule of thumb: fetch to inspect, pull to apply.
Diff and review
git diff
Shows unstaged differences.
git diff --staged
Shows staged differences.
Undoing changes (careful)
git restore file.txt
Discards local changes in a file.
git reset HEAD file.txt
Removes file from staging area.
git reset --hard
DANGER: Deletes all uncommitted local changes.
Safe undo for shared history
git revert <commit-hash>
Creates a new commit that undoes a previous one.
Stash (temporary save)
git stash
Saves local changes temporarily.
git stash pop
Restores the last stash.
Extra stash commands
git stash list
git stash apply
Recommended workflow
git checkout main
git pull
git checkout -b feature-x
git add .
git commit -m "Add feature X"
git push -u origin feature-x
Create a Pull Request on GitHub to merge into main.
Best practices
- Do not work directly on
main. - Pull before starting work.
- Use clear, concise commit messages.
- One feature or fix per branch.
Common emergency commands
Resolve merge conflicts
- Edit conflicted files.
- Resolve manually.
- Stage and commit.
git status
git add .
git commit -m "Resolve merge conflicts"