Giter Club home page Giter Club logo

git-test's Introduction

Cheatsheet: Git & GitHub

First of all, what's the difference?

Git is a version control system while GitHub is a service which lets you host Git repositories online for widespread collaboration.

Why would you need Git?

Simple. When you need to go back in time to when your code was actually working, you would be thankful you used Git to track the changes you made.

Also the next time a unit asks you to make changes to the same project for different tasks, you won't need to copy and paste the folder 7 times. You can use Git branches for that instead and get a cool visualisation like this:

VS Code GitLens visualisation

Terminology:

Before we start, make sure to learn the super cool Git lingo!

  • directory - folder
  • repository (repo) - main folder with all code files
  • remote - remote copy hosted on GitHub or similar service
  • clone - local machine copy of the repo
  • commit - a snapshot of changes recorded at a certain point in time
  • branch - a separate line of development with its unique history of changes or commits
  • staging area - when you add files to this area, they are tracked by Git and ready to be committed
  • fork - your copy of the someone else's repo forked on GitHub to your profile
  • CLI / Bash / Shell / Terminal / Console - if you're on Windows this means Git Bash, any other OS just open your terminal or use the integrated Visual Studio Code terminal

Installation

Download the Git installer at https://www.git-scm.com/downloads. MacOS users, I recommend installing Git through HomeBrew.

GitHub Desktop is available if you want to use a graphical Git client. Visual Studio Code and many IDEs also have in-built Git (aka Source Control) functionality.

But I highly recommend, using the command-line interface (CLI) as it is much faster to work with and gives you a lot more control and functionality.

Windows users can use Git Bash for this which comes with the Git installation. MacOS and Linux users can use the default terminal that comes with the OS.

To get a visualisation of your Git repo like shown above (and so many other cool features), install the GitLens extension in Visual Studio Code.

Active Learning

Before you continue, fork this repository to your own GitHub account using the Fork button at the top right. This will allow you to experiment and make changes to the repo using what you will learn below.

After forking, clone the repo to your local machine using the green Code button at the top right. Click on the SSH tab and copy the URL. You will find the Git command you need to do this below.

Make sure you have configured your GitHub account with a unique SSH key following these guides:

  1. Generate an SSH key pair
  2. Add the SSH key to your GitHub account

Now, you will have an environment where you can freely experiment with the Git commands below. Don't be afraid and explore! There's no better way to learn.

I will be pushing changes as well so you can practice pulling changes from the remote (this original repo). Another good way to practice would be contributing to open soure projects!

Commands

  • git init

    • initialises a .git directory inside the current folder
    • useful if you're starting locally first before uploading to GitHub
  • git clone <remote_url>

    • makes a local copy of the remote repo
    • you can get the <remote_url> through GitHub (green Code button in your repo)
  • git add <file_path>

    • adds the file to the staging area, telling Git to track the file for when you commit next
    • git add . : adds all files
  • git commit -m "feat: add Git cheatsheet"

  • git status

    • check which files have changes in them
    • shows which files are tracked and untracked by Git
    • shows if the local branch is ahead or behind the remote branch
  • git log

    • shows the commit history
    • useful to copy the commit hash (long string of alphanumeric characters beside the word commit)
  • git checkout <branch_name> or git switch <branch_name>

    • switch to a different branch
    • git checkout -b <branch_name> : create a new branch and switch to it
  • git remote -v

    • show all configured remote repositories (name and url)
    • git remote add <name> <url> : add a remote
    • git remote set-url <name> <url> : change the url of a remote
  • git push

    • push the new commits that have been made locally to the remote branch
    • git push -u <remote_name> <branch_name> : set the local branch to track the remote branch specified and push the changes, useful when the branch is newly created locally
  • git pull

    • pull any new commits from the remote branch
  • git fetch

    • download Git data (ojects and references) from the remote/s
    • git fetch --all --prune : fetch from all remotes and remove any references to objects that no longer exist in the remote
  • git merge <branch_name>

    • merges the unique commits from the specified branch into the current branch you are in
  • git cherry-pick <commit_hash>

    • apply just the specified commit to the current branch
  • git reset --hard <remote_name>/<remote_branch>

    • reset your Git history to match the history of the specified remote branch
  • git fetch <remote_name> pull/<pull_request_id>/head:<branch_name>

    • creates a new branch with the changes in the specified pull request made to the remote
    • get the pull request ID from the GitHub pull request URL e.g. https://github.com/username/remote-repo/pull/<id>
    • checkout the created branch to review the pull request changes

Useful Resources

The Odin Project Git Guides:

  1. Setting up Git
  2. Introduction to Git
  3. Git Basics

They link to lots more useful resources as well at the bottom so check them out!

Pro Git Book: read online for free - very simple and informative

Git Documentation: collection of resources - manual, videos, external links

Don't be afraid to Google or ask ChatGPT. There is an infinite amount of resources available that simplify everything to the dot!

Extra: Useful Unix Shell commands

When using Git on the CLI, it helps a lot to know how to do things like moving into different folders fast using just terminal commands.

Note: Unix-based systems like MacOS and Linux will let you execute these commands using the built-in terminal. If you're using Windows, use Git Bash for this as it will be using the Bash shell which fully supports these commands unlike Command Prompt.

I highly recommend completing this fun short course on the Unix Shell and reading through this Command Line Basics article by The Odin Project if you have time. Would also help to know your operating system's directory structure.

Try out these commands yourself while reading to see the output!

  • pwd
    • prints the file path of the current working directory or the folder you are currently executing terminal commands in
  • ls
    • lists all the files and folders in the current working directory
    • ls -a : includes hidden files and folders
    • ls <path> : output contents of directory specified by path
  • cd <path_to_folder>
    • changes current working directory to another folder
    • e.g. cd ~/Downloads
    • cd .. : goes back to the parent folder
    • cd ~ : goes to the home folder
    • cd / : goes to the root folder
  • mkdir <folder_name>
    • creates a folder with the given name in the current working directory
  • touch <filename>
    • creates an empty file in the current working directory
    • e.g. touch README.txt
  • nano <filename>
    • opens the file in the built-in terminal text editor called nano
    • creates a file if the specified file does not exist
    • use ctrl + x to exit nano
  • cat <filename>
    • outputs the contents of the file to the terminal
  • mv <filename> <to_path>
    • moves the file or folder to the specified path or folder
    • mv <old_filename> <new_filename> : renames the file, can also be done to folders
  • cp <filename> <to_path>
    • makes a copy of the file in the specified path or folder
    • cp -r <folder_name> <to_path> : copies folder and everything in it
  • rm <filename>
    • deletes the file
    • rm -r <folder_name> : deletes the folder and everything in it

The <filename> and <folder_name> arguments specified above can take file paths as well e.g. ~/Downloads/unit/test.txt.

Feedback

Make a GitHub Issue with any feedback or comments on what can be added to this guide!

If you have anything you'd like to contribute, make a pull request to this repo from your fork as well! This would be a great way to practice making pull requests and reviewing them.

Let's get the collaboration going!

Fun Stuff

git-test's People

Contributors

satikaj avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.