Henry_Hamlet
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# Git Basics [![HackMD Version](https://img.shields.io/badge/Made%20with-Markdown-1f425.svg)](https://hackmd.io/LobT9mNYQJCZ3b7TbnDRQA?view) #### Author: Henry Loi (hchloi@connect.ust.hk) *Reference from HKUST-Robotics-Team-SW-Tutorial-2021 by Johnathan and Elwin* This document aims to provide the reader with a sense of how the git version control system works and provide a simple, hands-on demonstration. Git is complex and this tutorial only demonstrates the basic essentials. ## Overview ### What is Git and GitHub? - Git is a free and open source distributed **version control system** designed to handle everything from small to very large projects with speed and efficiency. - GitHub is designed as a **Git repository hosting service**. You can share your code and giving other users to make revisions and edits. ### Basic Workflow ![](https://i.ytimg.com/vi/0nqJKEh3YCc/maxresdefault.jpg) 0. You and your friend clone a repository from GitHub. A *local copy* now exists on both your machines. 1. You make changes in your IDE. 2. You **add** and **commit** those changes. The *local* repo now registers this commit. 3. You **push** your changes. The *online* (aka remote) repo now shows these changes. 4. Your friend **pulls** your changes. Their *local copy* now shows those changes. To clarify, before pulling, your friend still had the copy from the beginning (no change). But after pulling, the local copy is updated. ### Conflict/Merge Workflow 0. You and your friend clone a repository. 1. You **and your friend** make changes. 2. You add and commit your changes. 3. Your friend **add**s and **commit**s their changes. 4. You push your changes. 5. Your friend **pulls** your changes. 6. Your friend **merges** the code. This may require a closer inspection and decision into each conflict. ## Terminology Most definitions provided simplify the concepts a bit in an attempt to make them digestable. (Some were copied from online.) Google the term for more details (e.g. "git push"). ### Repository (repo) A centralised place to store files and resources. (Kinda like Google Drive or Dropbox.) ### Commit (n.) A commit is a revision, an individual change to a file (or a set of files). These revisions are tracked by a unique ID (a hash). To commit (v.) a change (or a file, or something) means to create a commit (n.). In most cases, this commit will only be stored in the local repository until you push. ### Clone Copy the contents of a repository (normally to your local side). ### Checkout Navigate between branches and commits. Your local repository will be updated. ### Push Upload your local commits to the remote (online) repository. ### Pull Download the remote repository and update your local repository. ### Branches Branches allow you to make commit and push while maintaining independence from other people's commits. ![](https://www.nobledesktop.com/image/gitresources/git-branches-merge.png) You can think of the default branch (normally called *master*) as the tree trunk, and other branches would branch off and diverge with their own changes. ### Merge When two branches are combined together. The changes from the incoming branch is applied to the current branch. If a file could not be resolved, a **merge conflict** will occur. ## Working There are multiple ways of working with Git. If your repo is on Github, you can edit text files directly in the Github editor. However, it is often more convenient to work with a local IDE. Locally, you can directly use Git commands in the terminal. Another (often visually more convenient) way is to use a Git client, which is typically software designed to interface with the Git terminal commands. Some examples of Git clients are: * [Github Desktop](https://desktop.github.com/): Minimal, works well with Github repos * [Sourcetree](https://www.sourcetreeapp.com/): Complete, many buttons * Git Graph: VSCode extension * Sublime Merge * Git Kraken You can try them out to see which one suits you. For the purpose of this document, we'll stick to Github Desktop. Any other client should be able to do what Github Desktop can do. If you're using the terminal, you can find documentation for a command by typing `man git <command>` or `git <command> --help`. You can try out the following steps by creating a new repo solely for testing and playing around with. (To create a repo, click the <kbd>+</kbd> icon at the top right.) For the shell examples, we'll be using a fake `Example` repo. You can replace this with a repo of your choice. ### Setup Workflow There are several ways to setup a Github repository. Here we'll just focus on cloning a repository from online. First you'll want to clone the repository. #### 1. Github Desktop (Suggested) 1. Click the green <kbd>Code</kbd> button. 2. Click <kbd>Open with Github Desktop</kbd>. ![](https://i.imgur.com/VOGlO0e.png) 3. Specify a local path (default is ok). 4. Press <kbd>Clone</kbd>. ![](https://i.imgur.com/NXuXP2z.png) #### 2. Shell This shell command will clone the repository to a folder in the current directory. The folder will have the same name as the repo. ```sh git clone https://github.com/HKUST-Robotics-Team/Example ``` You can add a second path to specify a folder to clone to. ```sh git clone https://github.com/HKUST-Robotics-Team/Example path/to/your/repo ``` ### Development Workflow - Commit Perhaps you've made some changes on the local side. Maybe you edited some code, moved some files or deleted some junk. It's time to sync up the remote repo with your local copy. But first we need to update our local repository with the changes. Let's make a commit. #### 1. Github Desktop (Suggested) 1. Check which files you want to commit. 2. Write a commit message. This is sometimes pre-generated. 3. Write a commit description (optional). 4. Press <kbd>Commit to ...</kbd>. (Ctrl/Cmd + Enter) ![](https://i.imgur.com/SE5H53a.png) #### 2. Shell At this point, it is crucial that your current directory is in the repo. ```sh cd path/to/your/repo ``` You can run `pwd` to check that you're in the right folder and run `git status` to check that you're in a git environment. > ``` > % pwd > /Users/Robotics/Documents/path/to/your/repo > > % git status > On branch main > Your branch is up to date with 'origin/main'. > > nothing to commit, working tree clean > ``` When making commits, generally the process follows: ```sh git add . git commit -m "(sensible message, preferrably describing what you changed and why)" ``` `git add` *stages* your files, selecting them for a commit. The files haven't been committed yet, but they have been selected. We git add `.` to add modified files in the *current directory*. You can be more selective and select individual files or folders by specifying them (e.g. `git add main.cpp`, `git add some-folder/`). `git commit` *commits* your staged files. We use the `-m` flag, followed by a string, to write a commit message. Typically, commit messages should describe what changes you've made in the commit and why you've made them. ### Development Workflow - Push Now you want to upload your changes to the remote repository. #### 1. Github Desktop 1. Press the push button in the top row. (Ctrl/Cmd + P) 2. If there are no merge conflicts and you have a stable connection, you're done! ![](https://i.imgur.com/swtCg3E.png) #### 2. Shell Simplest command: ``` git push ``` If there are no merge conflicts and you have a stable connection, you're done!

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully