Skip to content

EPFL-ENAC/git-intro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Git intro

Note

This is a short practical introduction to collaborative Git fundamentals. For the theoretical part, see the slides.

Go get started:

  1. Create a GitHub account.
  2. Fork this repository.
  3. Install Git on your computer.
  4. Clone your forked repository:
git clone <your-repository-url>
cd <your-repository-name>
  1. Check the URL of the remote repository:
git remote -v

This 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.

Configuration

  1. Set your name and email address.
git config --global user.name "Your Name"
git config --global user.email "your.email@something.com"
  1. 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

Exercise 1: Commit

  1. Make a change in file_1.md.
  2. Check the status of the repository.
git status
  1. Check the changes made in the file.
git diff
  1. Add the file to the staging area.
git add file_1.md
  1. Commit the changes.
git commit -m "put a descriptive message here"
  1. Push your local changes to the remote repository.
git push
  1. Check the current tree of commits.
git log --all --decorate --oneline --graph

Tip

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"

Exercise 2: Checkout

  1. Checkout to the previous commit.
git checkout HEAD~
  1. 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?
  2. Check your current position in the tree of commits, indicated by (HEAD).
git log --all --decorate --oneline --graph
  1. To go back to the main branch, do:
git checkout main
  1. Look at the content of file_1.md again. What do you see?
  2. 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.

Exercise 3: Branch

  1. Create a new branch from the commit of the previous exercise.
git branch new-branch
  1. List all the branches.
git branch
  1. Switch to the new branch.
git checkout new-branch
  1. Make a change in file_2.md.
  2. Commit your changes.
  3. Look at the current tree of commits.
git log --all --decorate --oneline --graph

Tip

The git branch new-branch and git checkout new-branch commands can be combined into a single command:

git checkout -b new-branch

Bonus step: Try to push new-branch to the remote repository (origin). Note that new-branch does not exist on the remote repository yet.

Exercise 4: Merge

  1. Make sure that all changes are commited on new-branch. Running git status should show nothing to commit, working tree clean.
  2. Switch back to the main branch.
git checkout main
  1. Merge the changes from new-branch to main.
git merge new-branch
  1. Look at the current tree of commits.

Bonus Exercise: Merge Conflict

  1. Create a branch conflict-branch from main.
  2. Make a change in file_1.md in conflict-branch and commit it.
  3. Switch back to main and commit a different change in file_1.md that conflicts with the change commited in conflict-branch (e.g. change the same line).
  4. Merge conflict-branch to main and resolve the conflict.

Exercise 5: Pull Request

  1. Checkout the main branch and push it to the remote repository.
  2. Go to the GitHub page of your forked repository.
  3. Create a pull request from your main branch to the original repository's main branch.
  4. Add a description and comments to the pull request.

Additional Resources

About

Practical introduction to collaborative Git fundamentals

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Contributors