Here's a step-by-step plan to help you transition from Subversion (SVN) to Git, along with exercises to reinforce your learning.
- Git is decentralized, while SVN is centralized.
- Git uses snapshots of the project’s file system, whereas SVN tracks changes at the file level.
- In Git, branching is lightweight and encouraged, whereas in SVN, it is more heavyweight.
Exercise:
- Write down the key differences between Git and SVN in your own words.
- Try visualizing the workflows (SVN's central server vs. Git's local repository and remote).
- Install Git on your machine: Git installation instructions.
- Set up your user name and email in Git:
git config --global user.name "Your Name" git config --global user.email "[email protected]"
Exercise:
- Create a local Git repository:
mkdir my-git-project cd my-git-project git init
- Add a simple file (e.g., README.md), stage, and commit it:
echo "# My Git Project" >> README.md git add README.md git commit -m "Initial commit"
- Staging area: Git uses a staging area to organize commits.
- Committing: Saving snapshots of your project.
- Basic Commands:
git add
git commit
git status
git log
Exercise:
- Modify the README file, add the changes, and commit again.
- Use
git log
to view your commit history.
- Git's branching model is flexible and easy to use.
- Create, switch, and merge branches frequently to manage different features.
Exercise:
- Create a new branch:
git checkout -b new-feature
- Make changes to a file, commit them, and switch back to the
main
branch:git checkout main git merge new-feature
- Learn how to interact with remote repositories.
- Push your changes to a remote repository and pull updates from others.
git push
andgit pull
are the core commands.
Exercise:
- Create a GitHub/GitLab account if you don’t have one.
- Create a remote repository and push your local project to GitHub:
git remote add origin https://github.com/yourusername/your-repo.git git push -u origin main
- Learn how to handle merge conflicts when working with multiple branches or collaborators.
- Practice resolving conflicts manually.
Exercise:
- Create two branches, make conflicting changes to the same file in both, and try to merge them:
git checkout -b branch1 # Make changes and commit git checkout -b branch2 # Make conflicting changes and commit git checkout main git merge branch1 git merge branch2 # This will create a conflict
- Understand the difference between
git merge
andgit rebase
. - Rebase can simplify your commit history but may require more care when working with others.
Exercise:
- Create a branch, make some commits, then rebase it onto the main branch:
git checkout -b feature-branch # Make commits git checkout main git rebase main feature-branch
- Learn how to save your work temporarily using
git stash
, useful when you need to switch branches quickly.
Exercise:
- Modify a file without committing, then stash the changes:
git stash git stash pop # To apply stashed changes
- Use interactive rebase (
git rebase -i
) to clean up your commit history by squashing multiple commits into one.
Exercise:
- Make several commits, then use
git rebase -i
to squash them into a single commit.
- Learn how to fork a repository, clone it locally, make contributions via pull requests, and stay up to date with the upstream repository.
Exercise:
- Fork a simple open-source project on GitHub.
- Clone it locally, make a small change, and create a pull request.
- Learn about popular Git workflows:
- GitFlow
- GitHub Flow
- Forking Workflow
Exercise:
- Read about Git workflows and experiment with one that fits your style.
- Practice working with multiple collaborators, resolving conflicts, and creating pull requests.
Exercise:
- Pair with a colleague, clone the same project, and work on different features, then merge and resolve conflicts together.
This plan will guide you from beginner to intermediate Git skills. Feel free to adjust the pace based on your comfort level, and don't hesitate to experiment!