Git & GitHub Complete Guide: Essential Commands for Developers

A practical Git and GitHub reference covering installation, repositories, commits, branches, merging, rebasing, stash, remotes, pull requests, undoing changes, troubleshooting, and everyday commands.

Git & GitHub Complete Guide: Essential Commands for Developers

Git & GitHub Complete Guide

Git is a distributed version control system used to track changes in software projects. GitHub provides hosting and collaboration features around Git repositories.

This guide focuses on the commands developers use most often.


1. Check Git Installation

git --version

If Git is installed, you'll see the installed version.

Git's official documentation provides installation instructions for Windows, macOS and Linux.


2. Configure Git

Set your name:

git config --global user.name "Your Name"

Set your email:

git config --global user.email "you@example.com"

View configuration:

git config --list

3. Create a Repository

Navigate to your project:

cd my-project

Initialize Git:

git init

Check the repository:

git status

4. Clone an Existing Repository

git clone https://github.com/user/project.git

Then:

cd project

5. Check Changes

git status

See detailed changes:

git diff

See staged changes:

git diff --staged

6. Stage Files

Stage one file:

git add index.js

Stage multiple files:

git add index.js app.js

Stage everything:

git add .

7. Commit Changes

git commit -m "Add authentication flow"

View commits:

git log

Compact history:

git log --oneline

8. Branches

List branches:

git branch

Create a branch:

git branch feature-auth

Switch:

git switch feature-auth

Create and switch:

git switch -c feature-auth

Git also supports the older:

git checkout -b feature-auth

9. Merge

Switch to the branch receiving the changes:

git switch main

Update it:

git pull

Merge:

git merge feature-auth

10. Remote Repositories

View remotes:

git remote -v

Add a remote:

git remote add origin https://github.com/user/project.git

Push:

git push origin main

Push a new branch:

git push -u origin feature-auth

11. Pull Changes

git pull

Download remote changes without merging:

git fetch

Git's reference groups fetch, pull, push, branches, merge, stash, reset, restore and related commands as core parts of everyday Git usage.


12. Git Stash

Temporarily save changes:

git stash

List stashes:

git stash list

Restore latest stash:

git stash pop

Apply without removing:

git stash apply

13. Undo Changes

Discard changes in a file:

git restore filename

Unstage a file:

git restore --staged filename

Reset the latest commit while keeping changes staged:

git reset --soft HEAD~1

Reset the latest commit while keeping changes in the working tree:

git reset --mixed HEAD~1

Hard reset:

git reset --hard HEAD~1

Be careful with --hard because it can permanently remove uncommitted work.


14. Git Rebase

Update your feature branch with the latest main branch:

code
git switch feature-auth
git fetch origin
git rebase origin/main

If conflicts occur:

git status

Fix the files, then:

code
git add .
git rebase --continue

Abort:

git rebase --abort

15. Find a Commit

Search commits:

git log --oneline

Show a commit:

git show <commit>

Search by message:

git log --grep="authentication"

16. Useful Everyday Workflow

code
git pull
git switch -c feature-name

# make changes

git status
git add .
git commit -m "Add feature"
git push -u origin feature-name

Then create a Pull Request on GitHub.


Git Cheat Sheet

code
git init
git clone
git status
git add
git commit
git log
git diff
git branch
git switch
git merge
git rebase
git stash
git restore
git reset
git remote
git fetch
git pull
git push
git show
git tag

For the complete official command reference, see Git's documentation.