Skip to content

3. Git Tutorial

Joseph Saliba edited this page Oct 26, 2023 · 4 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.

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

TODO: MODIFY

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.