1. Create
From existing data
When starting a new project locally:
cd ~/my_project_directory
# Navigate to your project folder
git init
# Initialize an empty git repository
git add .
# Add all files to staging
From an existing repository
When collaborating on an existing project:
git clone /existing_repo ~/new/repo
git clone git://host.org/project.git
git clone ssh://user@host.org/project.git
2. Show
# Files changed in working directory: Shows modified, added, or deleted files.
git status
#Changes made to tracked files: See what lines of code have changed before staging or committing.
git diff
#Compare two commits:
git diff <ID1> <ID2>
# View commit history: Check who made changes and when.
git log
# Inspect a specific commit:
git show <ID>
# List local branches:
git branch
# Tip: Use git diff for unstaged changes. Use git log and git show for understanding history and tracking changes.
3. Revert
# Return to the last committed state (WARNING: irreversible):
git reset --hard
# Undo the last commit without losing changes: Creates a new commit that undoes the last commit.
git revert HEAD
# Modify the last commit message or add forgotten changes:
git commit --amend
# Get back a previous version of a file:
git checkout <ID> <FILE>
Difference between
git reset --hardandgit revert HEAD:
git reset --hardpermanently discards changes (use with caution).git revert HEADsafely undoes a commit by adding a new one (preferred in collaborative settings).
4. Update
# Fetch latest changes from the remote (without merging): Updates local metadata but doesn't affect your working files.
git fetch
e.g git fetch origin
# Fetch and merge changes in one step:
git pull
e.g git pull origin main
# Apply a patch file:
git am -3 patch.mbox
# Mark conflicts resolved in patch application:
git am --resolved
Difference between git fetch and git pull:
git fetchjust downloads the latest changes (use when you want to review before merging).git pulldownloads and merges (use when you trust upstream and want to integrate immediately).
5. Branch
# Switch branches:
git checkout <BRANCH>
# Merge another branch into your current branch:
git merge <BRANCH>
# Create a new branch:
git branch <BRANCH>
# Delete a branch:
git branch -d <BRANCH>
# Tip: Create and switch to a new branch in one step using git checkout -b <BRANCH>.
6. Resolve Merge Conflicts
# View conflicts after merge attempts:
git diff
# Discard the conflicting changes:
git reset --hard
git rebase --skip
# Resolve conflicts and continue the merge:
git add <CONFLICTING_FILE>
git rebase --continue
When to use git reset --hard vs git rebase --skip:
git reset --hardwill wipe out local changes.git rebase --skipignores the problematic commit and continues rebasing.
7. Publish
# Commit staged changes:
git commit -a
# Create patches to share changes via email or another system:
git format-patch origin
# Push changes to the remote repository:
git push
# Tag a specific release version:
git tag v1.0
8. Workflow Explained
Step-by-step flow:
CREATE: Start by
initorclone.BROWSE: Use
status,log,show,diff,branchto inspect.CHANGE: Make code edits.
REVERT: Use
reset,checkout,revertif things go wrong.UPDATE: Integrate changes using
pull,fetch,merge,am.BRANCH: Switch or create branches for safe experimentation.
COMMIT: Save changes with
commit.PUBLISH: Share with the world using
pushandformat-patch.



