Note
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>
cd <your-repository-name>- Check the URL of the remote repository:
git remote -vThis should show you the URL of your forked repository, such as:
origin git@github.com:<your-username>/git-intro.git (fetch)
origin git@github.com:<your-username>/git-intro.git (push)
If you see EPFL-ENAC in place of <your-username>, it means you cloned the original repository instead of your forked one. You won't have the rights to push your changes to it! Go back to step 4 and make sure you clone your fork.
- Set your name and email address.
git config --global user.name "Your Name"
git config --global user.email "your.email@something.com"- Git uses Vim as its default text editor, for example when writing commit messages. If you are not familiar with Vim, you can change the default editor to another one, for example Nano:
git config --global core.editor nano- 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- Check the current tree of commits.
git log --all --decorate --oneline --graphTip
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"- Checkout to the previous commit.
git checkout HEAD~- Git will tell you that your are now in detached HEAD state, meaning that you are no longer attached to a branch. 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 --graph- To go back to the
mainbranch, do:
git checkout main- Look at the content of
file_1.mdagain. What do you see? - You can also checkout to a specific commit by using its hash. Look at the tree of commits (
git log) and checkout to a previous commit.
- 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 --graphTip
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 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 a 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