Data Science Exam  >  Data Science Notes  >  The Course: Complete Bootcamp  >  CheatSheet:Version Control

CheatSheet:Version Control

1. Git Fundamentals

1.1 Core Concepts

Term Definition
Version Control System that records changes to files over time, enabling retrieval of specific versions later
Repository (Repo) Storage location containing project files and complete version history
Commit Snapshot of changes with unique identifier (SHA-1 hash), message, author, and timestamp
Working Directory Local folder containing current versions of project files
Staging Area (Index) Intermediate area where changes are prepared before committing
Branch Parallel version of repository allowing independent development lines
HEAD Pointer referencing current branch and most recent commit

1.2 Git Three-Tree Architecture

  • Working Directory: Modified files not yet staged
  • Staging Area: Files prepared for next commit (use git add)
  • Repository (.git directory): Committed snapshots stored permanently

1.3 Git Configuration

Command Purpose
git config --global user.name "Name" Set username for all repositories
git config --global user.email "email" Set email for all repositories
git config --list Display all configuration settings

2. Basic Git Commands

2.1 Repository Initialization

Command Function
git init Initialize new Git repository in current directory
git clone [url] Copy remote repository to local machine

2.2 Tracking Changes

Command Function
git status Show working directory and staging area status
git add [file] Stage specific file for commit
git add . Stage all changed files in current directory
git add -A Stage all changes (new, modified, deleted) in entire repository
git commit -m "message" Commit staged changes with descriptive message
git commit -am "message" Stage and commit all tracked files in one command

2.3 Viewing History

Command Function
git log Display commit history with full details
git log --oneline Show condensed commit history (one line per commit)
git log --graph Display branch and merge history visually
git diff Show unstaged changes between working directory and staging area
git diff --staged Show differences between staging area and last commit
git show [commit] Display details and changes of specific commit

2.4 Undoing Changes

Command Function
git checkout -- [file] Discard changes in working directory (restore from staging area)
git reset [file] Unstage file while keeping working directory changes
git reset --soft [commit] Move HEAD to commit, keep staging area and working directory unchanged
git reset --mixed [commit] Move HEAD to commit, reset staging area, keep working directory
git reset --hard [commit] Move HEAD to commit, reset staging area and working directory (destructive)
git revert [commit] Create new commit that undoes changes from specified commit

3. Branching and Merging

3.1 Branch Operations

Command Function
git branch List all local branches (current branch marked with *)
git branch [name] Create new branch without switching to it
git checkout [branch] Switch to specified branch
git checkout -b [branch] Create new branch and switch to it immediately
git branch -d [branch] Delete branch (only if merged)
git branch -D [branch] Force delete branch (even if unmerged)
git branch -m [old] [new] Rename branch

3.2 Merging Strategies

Command Function
git merge [branch] Merge specified branch into current branch
git merge --no-ff [branch] Create merge commit even for fast-forward merges
git merge --abort Cancel merge and return to pre-merge state

3.3 Merge Types

Type Description
Fast-Forward Branch pointer moves forward when no divergent commits exist
Three-Way Merge Creates merge commit combining changes from two divergent branches
Merge Conflict Occurs when same file section modified in both branches; requires manual resolution

3.4 Conflict Resolution

  • Open conflicted files (marked with <, =="====,">>>>>>)
  • Edit file to resolve conflicts manually
  • Remove conflict markers
  • Stage resolved files: git add [file]
  • Complete merge: git commit

3.5 Rebasing

Command Function
git rebase [branch] Move current branch commits to tip of specified branch
git rebase -i [commit] Interactive rebase allowing commit editing, squashing, reordering
git rebase --continue Continue rebase after resolving conflicts
git rebase --abort Cancel rebase and return to original state

3.6 Merge vs. Rebase

Merge Rebase
Preserves complete history Creates linear history
Creates merge commits No merge commits
Safe for public branches Never rebase public branches
More complex history graph Cleaner, simpler history

4. Remote Repositories

4.1 Remote Configuration

Command Function
git remote List remote repository names
git remote -v List remotes with URLs
git remote add [name] [url] Add new remote repository
git remote remove [name] Remove remote repository
git remote rename [old] [new] Rename remote

4.2 Synchronization Commands

Command Function
git fetch [remote] Download remote changes without merging
git pull [remote] [branch] Fetch and merge remote changes (fetch + merge)
git pull --rebase Fetch and rebase local commits on top of remote
git push [remote] [branch] Upload local commits to remote repository
git push -u [remote] [branch] Push and set upstream tracking branch
git push --force Overwrite remote history (use with caution)
git push --delete [remote] [branch] Delete remote branch

4.3 Tracking Branches

Command Function
git branch -r List remote branches
git branch -a List all branches (local and remote)
git checkout -b [branch] [remote]/[branch] Create local branch tracking remote branch
git branch -vv Show tracking branch information

4.4 Common Remote: origin

  • Default name for cloned remote repository
  • Automatically created during git clone
  • Points to source repository URL

5. GitHub Workflow

5.1 Fork and Pull Request

Step Action
1. Fork Create personal copy of repository on GitHub
2. Clone Download forked repository to local machine
3. Branch Create feature branch for changes
4. Commit Make and commit changes locally
5. Push Upload changes to forked repository
6. Pull Request Request original repository to merge changes

5.2 Syncing Fork with Upstream

Command Function
git remote add upstream [url] Add original repository as upstream remote
git fetch upstream Fetch changes from original repository
git merge upstream/main Merge upstream changes into local branch
git push origin main Update fork with upstream changes

5.3 SSH Key Setup

  • Generate SSH key: ssh-keygen -t ed25519 -C "email@example.com"
  • Start SSH agent: eval "$(ssh-agent -s)"
  • Add key to agent: ssh-add ~/.ssh/id_ed25519
  • Copy public key: cat ~/.ssh/id_ed25519.pub
  • Add to GitHub: Settings → SSH and GPG keys → New SSH key

6. Advanced Git Operations

6.1 Stashing

Command Function
git stash Save uncommitted changes temporarily
git stash save "message" Stash with descriptive message
git stash list Show all stashed changes
git stash apply Reapply most recent stash
git stash apply stash@{n} Apply specific stash
git stash pop Apply and remove most recent stash
git stash drop stash@{n} Delete specific stash
git stash clear Delete all stashes

6.2 Tagging

Command Function
git tag List all tags
git tag [name] Create lightweight tag at current commit
git tag -a [name] -m "message" Create annotated tag with metadata
git tag [name] [commit] Tag specific commit
git push origin [tag] Push specific tag to remote
git push origin --tags Push all tags to remote
git tag -d [name] Delete local tag

6.3 Cherry-Pick

Command Function
git cherry-pick [commit] Apply changes from specific commit to current branch
git cherry-pick [commit1] [commit2] Apply multiple commits
git cherry-pick --continue Continue after resolving conflicts
git cherry-pick --abort Cancel cherry-pick operation

6.4 Submodules

Command Function
git submodule add [url] [path] Add repository as submodule
git submodule init Initialize submodules in repository
git submodule update Fetch and checkout submodule commits
git clone --recursive [url] Clone repository including all submodules

7. .gitignore File

7.1 Purpose and Syntax

  • Specifies files and directories Git should ignore
  • Place .gitignore in repository root
  • One pattern per line
  • Blank lines and lines starting with # are ignored

7.2 Pattern Examples

Pattern Meaning
*.log Ignore all .log files
temp/ Ignore entire temp directory
!important.log Exception: do not ignore this file
**/logs Ignore logs directory anywhere in repository
doc/*.txt Ignore .txt files in doc directory only
doc/**/*.pdf Ignore .pdf files in doc and all subdirectories

7.3 Common Ignored Items

  • IDE configuration files (.vscode/, .idea/)
  • Dependency directories (node_modules/, venv/)
  • Build outputs (dist/, build/, *.pyc)
  • Environment files (.env, .env.local)
  • Log files (*.log)
  • Operating system files (.DS_Store, Thumbs.db)

8. Best Practices

8.1 Commit Messages

  • Use imperative mood ("Add feature" not "Added feature")
  • Keep subject line under 50 characters
  • Capitalize first letter
  • No period at end of subject line
  • Separate subject from body with blank line
  • Body explains what and why, not how

8.2 Branching Strategy

Branch Type Purpose
main/master Production-ready code
develop Integration branch for features
feature/[name] New feature development
bugfix/[name] Bug fixes
hotfix/[name] Critical production fixes
release/[version] Release preparation

8.3 General Guidelines

  • Commit early and often
  • Each commit should represent single logical change
  • Pull before pushing to avoid conflicts
  • Never commit sensitive data (passwords, API keys)
  • Review changes before committing (git diff)
  • Use branches for new features and experiments
  • Delete merged branches to keep repository clean
  • Never rebase public/shared branches
  • Test code before committing

8.4 Collaboration Tips

  • Sync with remote frequently
  • Communicate with team about branch work
  • Review pull requests thoroughly
  • Keep pull requests focused and small
  • Resolve conflicts promptly
  • Document significant changes in commit messages

9. Troubleshooting

9.1 Common Issues

Problem Solution
Committed to wrong branch git reset HEAD~1 (undo commit), checkout correct branch, recommit
Need to change last commit git commit --amend (modify most recent commit)
Accidentally deleted file git checkout HEAD [file] (restore from last commit)
Merge conflict Edit conflicted files, git add [file], git commit
Pushed wrong code git revert [commit] (safer) or git reset + git push --force (risky)
Detached HEAD state git checkout [branch] to return to branch

9.2 Viewing Repository State

Command Information
git status Current branch, staged/unstaged changes
git log --all --graph Complete branch structure visualization
git reflog History of HEAD movements (useful for recovery)
git ls-files List all tracked files

9.3 File Recovery

  • Restore file from specific commit: git checkout [commit] -- [file]
  • Find deleted file commit: git log --all --full-history -- [file]
  • Recover deleted commit: git reflog, then git checkout [commit]

10. Git Aliases

10.1 Creating Shortcuts

Command Creates Alias
git config --global alias.co checkout git co (for checkout)
git config --global alias.br branch git br (for branch)
git config --global alias.ci commit git ci (for commit)
git config --global alias.st status git st (for status)
git config --global alias.unstage 'reset HEAD --' git unstage (to unstage files)
git config --global alias.last 'log -1 HEAD' git last (show last commit)
The document CheatSheet:Version Control is a part of the Data Science Course The Data Science Course: Complete Data Science Bootcamp.
All you need of Data Science at this link: Data Science

Top Courses for Data Science

Related Searches
Free, past year papers, Previous Year Questions with Solutions, CheatSheet:Version Control, Important questions, Extra Questions, study material, pdf , ppt, practice quizzes, Summary, Sample Paper, Objective type Questions, CheatSheet:Version Control, Viva Questions, shortcuts and tricks, CheatSheet:Version Control, mock tests for examination, video lectures, Semester Notes, Exam, MCQs;