This is a short practical introduction to collaborative Git fundamentals. For the theoretical part, see the slides.
Go get started:
- Create a GitHub account.
- Fork this repository.
- Install Git on your computer.
- Clone your forked repository:
git clone <your-repository-url>- Make a change in
file_1.md. - Check the status of the repository.
git status- Check the changes made in the file.
git diff- Add the file to the staging area.
git add file_1.md- Commit the changes.
git commit -m "put a descriptive message here"- Push your local changes to the remote repository.
git push- Checkout to the previous commit.
git checkout HEAD~- Look at the content of
file_1.md. What do you see? - Check your current position in the tree of commits, indicated by
(HEAD).
git log --all --decorate --oneline --graphNote: Instead of typing the long git log command every time, you can create an alias for it (here git lg):
git config --global alias.lg "log --all --decorate --oneline --graph"- Create a new branch from the commit of the previous exercise.
git branch new-branch- List all the branches.
git branch- Switch to the new branch.
git checkout new-branch- Make a change in
file_2.md. - Commit your changes.
- Look at the current tree of commits.
git log --all --decorate --oneline --graphNote: The git branch new-branch and git checkout new-branch commands can be combined into a single command:
git checkout -b new-branchBonus step: Try to push new-branch to the remote repository (origin). Note that new-branch does not exist on the remote repository yet.
- Make sure that all changes are commited on
new-branch. Runninggit statusshould shownothing to commit, working tree clean. - Switch back to the
mainbranch.
git checkout main- Merge the changes from
new-branchtomain.
git merge new-branch- Look at the current tree of commits.
- Create a branch
conflict-branchfrommain. - Make a change in
file_1.mdinconflict-branchand commit it. - Switch back to
mainand commit a different change infile_1.mdthat conflicts with the change commited inconflict-branch(e.g. change the same line). - Merge
conflict-branchtomainand resolve the conflict.
- Checkout to the
mainbranch and push it to the remote repository. - Go to the GitHub page of your forked repository.
- Create a pull request from your
mainbranch to the original repository'smainbranch. - Add the description and comments to the pull request.
- https://learngitbranching.js.org/ Git branching for beginners
- Exercises on rewritting history
- Do you really know git? Lightning talk from fellow EPFL RSE