Git Introduction Workshop (Word)
- Distributed version control (tracks changes, supports collaboration, maintains history)
- Created in 2005 by Linus Torvalds, the creator of Linux
- Compared to SVN and Mercurial:
- Distributed architecture
- More flexible workflow
- Relevance for graduate students:
- Efficient tracking of research and development
- Enables collaboration
- Operates offline
- Industry-standard tool
Concept | Description |
---|---|
Repository (repo) | Project tracked by Git, can be local or remote (e.g., GitHub, GitLab). |
Commit | Snapshot of project state at a certain point, with descriptive message. |
Branch | Separate development line; used for features without disturbing main project. |
Merge | Combines changes from different branches. |
Clone | Copies a remote repository to your local machine. |
Push/Pull | Send commits to remote repository / Retrieve updates from remote repository. |
Fork | Personal copy of another user's repository on GitHub/GitLab. |
- Create GitHub account
- Install Git (Windows/Mac/Linux)
- Configure Git globally:
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"
- Create and clone repository from GitHub:
git clone <repo-link>
- Initialize local repository:
git init
- File states: Untracked, Staged, Committed
git add <file>
git add .
git commit -m "commit message"
git status
git log --oneline
- View unstaged changes:
git diff
- Diff between commits:
git diff <commit1> <commit2>
- Diff staged changes:
git diff --staged
- Check repository status:
git status
- Inspect commit details:
git show <commit>
- Enables isolated feature development
- Keeps main branch stable
- Create and switch branches:
git branch <branch-name>
git switch <branch-name>
- Shortcut to create and switch:
git switch -c <branch-name>
- Merge feature branch into main:
git switch main
git merge <branch-name>
- Merge methods:
- Fast-forward (simple merges)
- 3-way merge (complex merges)
- Resolve merge conflicts manually:
- Identify conflicts (
git status
,git diff
) - Edit conflicted files, remove markers, commit resolved changes
- Identify conflicts (
- Short-lived branches for features
- Clear, descriptive branch names
- Use graphical tools (
git log --graph --oneline --all
) for visualization
- Repositories on remote servers (GitHub)
- Supports collaboration via pull requests and issue tracking
- Create SSH keys:
ssh-keygen -t ed25519 -C "your_email@example.com"
cat ~/.ssh/id_ed25519.pub
- Add SSH key to GitHub under account settings
git remote add origin <URL>
git remote -v
- Push commits:
git push origin <branch-name>
- Fetch and merge remote changes:
git pull origin <branch-name>
- Fetch without merging:
git fetch
- Fork: Personal GitHub copy (common in open-source)
- Pull requests: Propose merging your forked changes into original repository
- Clone demo repository:
git clone https://github.com/radmanesh/git-workshop-demo-2025
- Create branches:
git switch -c groupA-branch # Group A
git switch -c groupB-branch # Group B
-
Modify
team.txt
:- Group A: Replace "Bob" with "Charlie"
- Group B: Replace "Bob" with "Dana"
-
Commit changes:
git add team.txt
git commit -m "Changed Bob to Charlie" # Group A
git commit -m "Changed Bob to Dana" # Group B
- Push changes:
git push origin groupA-branch
git push origin groupB-branch
- Merge on GitHub: Merge Group A’s PR to main
- Group B pulls main and faces conflicts:
git switch main
git pull origin main
git switch groupB-branch
git merge main # Resolve conflicts manually
- Occurs checking out specific commit directly
- Recover by switching to a branch:
git switch main # or
git switch -c new-branch
- Safely undo a commit:
git revert <commit>
- Rewrite history (with caution):
git reset --hard <commit>
- Ignore files properly:
.gitignore
file- Remove already-committed files:
git rm --cached <file>
- View repository history clearly:
git log --oneline --graph --decorate --all
- Document pull requests clearly with messages and comments for detailed collaboration.