Skip to content

4.1 Git Tutorial

Antoine Dangeard edited this page Sep 2, 2023 · 5 revisions

What is git

" Git is a distributed version control system that enables developers to track and manage changes in their codebase over time. It allows for easy branching, merging, and collaboration among team members, while maintaining a detailed history of commits and snapshots of the project.

"Imagine Git as a time machine for your codebase – a sophisticated tool that lets you travel through different versions of your project's history. Developed by Linus Torvalds, Git is like a collection of snapshots, each capturing a precise moment in your code's evolution. These snapshots, called "commits," are organized in a way that you can easily switch between them, helping you understand how your project has progressed over time. Just as a writer drafts and revises a manuscript, you can create new "branches" in Git, separate paths where you experiment with changes without altering the main storyline. When you're satisfied with your branch's narrative, you can skillfully "merge" it back into the main story. Git's unique approach to version control – distributed and collaborative – lets multiple writers (developers) create their own copies of the manuscript, make changes independently, and then synchronize their work together. Whether you're writing the story alone or collaborating with a team, Git's ingenious mechanism offers a clear path to crafting your code masterpiece while keeping track of every twist and turn along the way." - Chat GPT

branches

Remote vs Local

"Local" means the copy of your project on your computer, where you can work without the internet. "Remote" is the copy on a server, like GitHub. GitHub helps you collaborate and share your work with others. You use Git to sync your changes between your local and remote copies – pushing your changes to update the remote or pulling changes to update your local version.

Navigating Github

Front Page

Basics of Git

Basic git quick reference

  1. git add [file]: Stages a file for the next commit, marking it as ready to be saved.
  2. git commit -m "message": Records the staged changes in a commit along with a descriptive message.
  3. git status: Shows the current status of your working directory, indicating changed files and staged files.
  4. git log: Displays a chronological list of commits, including their messages and unique identifiers.
  5. git pull: Fetches changes from a remote repository and integrates them into your local branch.
  6. git push: Sends your local commits to a remote repository, updating the shared version.
  7. git checkout [branch]: Switches to a different branch, updating your working directory to match that branch's state.
  8. git branch -r: List all remote branches
  9. git clone [repository_url]: Creates a local copy of a remote repository on your computer.

Git Clone

Go to the home page of the repo on Github. Click the button highlighted in the green box in the diagram above. In the popup click https and copy the link. Open a terminal and navigate to the home directory. This can be done with the command cd ~. Enter the command git clone [THE LINK] to copy the code base to your machine. Run ls to verify that the cloning was successful. The AUV-2024 should be listed among the folders. ls

Git Pull

git pull grabs the latest changes from the remote servers and applies them to your local version. You should always run git pull before your start working to make sure you have the latest changes. If you forget to do this, you may have merge conflicts!

Git Checkout

git checkout [branch] is used to change branches. Running this command will change your local file system to reflect the status of that branch. Any changes you make will be added on top of the branch you changed to.

Creating a Branch

To create a new branch, run git checkout -b [branch-name]. This will create a branch off the current branch you are on. You can check your current branch with git status. This branch only exists on your local machine until you "push it" to the remote server. You can do this by running git push --set-upstream origin [branch-name].

Git Status

git status is a super useful command used to show the current status of your local version of the git repository. It displays information such as the current branch, files that have been changed, and files that have been staged. Screenshot from 2023-08-31 13-36-57 In the example image above, git status tells me that I just create to files in the root directory of the repository: "example1.txt" and "example2.txt". Any changes that have not been committed will appear in red.

Git Add

git add is used to specify which changed files you want to commit. Following the example above, if I wanted to commit the new file "example1.txt", but not "example2.txt", I would run git add example1.txt. Now running git status shows the following: Screenshot from 2023-08-31 13-42-00 Notice how "example1.txt" is now highlighted in green. This means that the changes to this file have been successfully added and are ready to be committed. Since we only added "example1.txt" and not "example2.txt", it remains highlighted red meaning those changes have not been added, and will not be committed.

Pro tip: When adding changes it's convenient to run git status, then copy the red text, and paste it in front of git add.

Further Pro Tip: To add all changes listed in git status at once, you can run git add -A.

Git commit

After adding all the changes you want for the new commit, run git commit -m "your-message-describing-the-commit-here". This will create a commit with all the staged changes (the changes in green).

Git Push

After creating a commit, it only exists on your local machine. To push your local changes to the remote server, run git push. Now when someone checks out your branch, it will have all the latest committed changes. Note: you cannot push to the main branch (noetic)! You must push to your own branch, merge the changes to the main branch.

Merging branches

After creating a feature on a new branch, to merge the new branch with the main branch, you have to make a pull request. This can be done from the repository's GitHub page. Navigate to the pull requests tab and click new pull request. You should end up on this page. Now, you can select which branches you want to merge. Pull requests to the main branch require a review from at least one other member.

Exercise

Checkout the tutorial-1-git branch and look at Anthony.txt in the root folder of the repository. In this file, I've said my name and my favourite robot. For this exercise, make a branch off of tutorial called tutorial-1-[your-name] and create your own txt file. You can write whatever you want in it. Feel free to also include your favourite robot. After creating this file, add it, commit it, push it, and open a pull request. IMPORTANT: Set the pull request to merge with tutorial-1-git, not noetic.