Advanced Git Workshop
Welcome to the Advanced Git and GitHub/GitLab Workshop at EPAM Systems!
1. Git Basics
What is a Git Repository?
- A Git repository is a version-controlled project folder.
- Every project (e.g., portfolio, app, website) should have its own repository.
- You can have multiple repositories on your machine, each with its own history.
Making a Commit
- A commit is like saving your work—it captures a snapshot of your project.
- Commits can include multiple files or folders.
Examples of files in a commit:
main.html,chatbot.js,mobile.css,logos/
Common Git Workflow
| Step | Action |
|---|---|
| Work | Make changes in your project |
| Stage | Select changes using git add |
| Commit | Save staged changes with git commit |
2. Working with Changes
Checking Repository Status
- Shows which files are staged, unstaged, or untracked.
git status
Tip: If you're not inside a Git repo, you'll see:
fatal: not a git repository
Starting a New Repository
- Initializes a Git repo in the current directory.
git init
Advice:
- Don't create a Git repo inside another one.
- Use
git statusto check. - Mistake? Delete the
.gitfolder.
Tracking Changes
- Git watches your files after you run
git init. - New files show up as untracked.
Staging Changes
- Moves selected changes to the staging area.
git add file1 file2 # Add specific files
git add . # Add everything
Committing Changes
- Saves all staged changes to the repository.
- A commit message is required.
git commit
git commit -m "Initial commit"
Reminder: Use git status often to see what's staged.
3. Managing Commits
Writing Inline Messages
git commit -m "Fix login bug"
Amending a Commit
- Fix the most recent commit (add files or change message).
git add missed_file
git commit --amend
Good Commit Practices
- Make frequent commits to keep history clear.
- Each commit should represent one logical change.
- Write clear, short messages.
Commit Message Style
- Use command-like style (e.g.,
Fix typo, notFixed typo). - Be consistent with your style.
4. Ignoring Files
When to Use .gitignore
- For files you don't want in version control, like:
- Secrets, credentials, API keys
- System files like
.DS_Store - Logs, dependency folders like
node_modules
Example:
.DS_Store
*.log
folderName/
5. Comparing Changes
What You Can Compare with git diff
- Working directory vs staging area
- Staging area vs last commit
- Specific commits or branches
Common Diff Commands
git diff # Unstaged changes
git diff HEAD # Changes since last commit
git diff --staged # Staged vs last commit
git diff commit1..commit2
git diff branchA..branchB
How to Read a Diff
- removed line
+ added line
@@ context indicator
- Changes are grouped into chunks.
- Headers show which lines changed.
Diffing a Specific File
git diff HEAD file.js
git diff --staged file.js
6. Saving Work Without Committing
Using Git Stash
- Temporarily save changes so you can switch tasks.
git stash # Save changes
git stash pop # Reapply and delete
git stash apply # Reapply without deleting
git stash -u # Include untracked files
Managing Multiple Stashes
git stash list
git stash apply stash@{2}
git stash drop stash@{2}
git stash clear
7. Undoing and Recovering Changes
Checking Out Commits
- Move between branches or revisit older commits.
git checkout <commit>
git switch master
git switch -c temp-branch
Referring to Previous Commits
git checkout HEAD~2 # Go back two commits
Reverting File Changes
git checkout -- file.js
git restore file.js
git restore --source HEAD~1 file.js
git restore --staged file.js
8. Resetting vs Reverting
git reset
- Undoes commits (used for private/local work).
git reset <commit>
git reset --hard <commit>
git revert
- Reverses changes by creating a new commit (safe for shared repos).
git revert <commit>
When to Use Each
| Command | Use Case | What It Does |
|---|---|---|
reset | For local/private changes | Alters commit history |
revert | For public/shared repos | Preserves history, adds new commit |