GitHub Basics - KSU-ART/tutorial-site GitHub Wiki

GitHub basics

(In general, you should use the internet to discover how to do specific things. This is just a simple guide for how to do most things correctly)

Starting Q&A

  • What is github?

It is "the" website used to back-up and share code. It allows you to easily work on a single project from multiple computers and with other people. It also saves the history of a project and lets you 'undo' any change.

  • What is a repository?

A repository -or repo- is a personalized website that you can upload your code to. You should usually make a separate repo for every large project that you start.

How Do I ...?

make a new repo

  1. go to github.com and sign in
  2. go to github.com/new
  3. name your repository
  4. write a description of your project
  5. click the checkbox for "initialize this repository with a README"
  6. add .gitignore for python
  7. add a liscense (MIT usually)

download a repo

  1. go to the folder you want to download it to
    1. open powershell (windows) or the terminal (linux)
    2. use cd to go to the folder you want
  2. git clone <https://github.com/username/reponame.git>
    1. (without the <>. <> just means change it to what you want.)

save changes

  1. git status
  2. git add <changed files>
  3. git status (the desired changes to upload should be green)
  4. git commit -m "<what changes you made>"
    1. you need to set your name and email for this to work

upload changes

  1. save changes
  2. git push

set up github on a new computer

  1. On Windows:
    1. Download the install kit from https://git-for-windows.github.io/
    2. open powershell and run commands:
      1. git config --global user.name "<my_github_name>"
      2. git config --global user.email "<my_github_email>"
  2. On linux:
    1. sudo apt install git
    2. git config --global user.name "<my_github_name>"
    3. git config --global user.email "<my_github_email>"

reset a repo to the online version

  1. Make sure you are OK with losing all offline changes you have made to the repo
  2. git reset --hard origin/master
    1. If you want to reset a different branch, change master to the name of that branch

GitHub In-Depth Commands

Using branches

Branches are copies of the repository that are used for developing specific features. It allows different parts of a project to be developed without it disrupting the whole project. Branches are merged into the master branch once work on a specific feature is complete.

To checkout an existing branch, go to a repo directory and run this command

git checkout <branch name>

To create a new branch, run this command

git checkout -b <new branch name>

To see what branch you are on, run this command

git branch

To upload a new branch to GitHub, run this command from the new branch

git push

Using logs

GitHub is a version control software. This means that it allows you to keep track of multiple versions of code and revert to previous versions in times of need. This allows you to undo any change by any contributor to a project.

To see the list of changes made to a project's source code, run

git log

This displays a bunch of hash numbers for every commit and information about who made the commit and when the commit was made. To reset to a specific commit:

git reset <the commit's hash number>

to undo changes made during a specific commit:

git revert <the commit's hash number>

See what GitHub is tracking

Red stuff is untracked, green stuff is tracked.

git status

Track changes to the code

When you are satisfied with a change, begin track it with

git add <path/to/fileOrDirectory>

Then, run

git status

To check if your change is being tracked. Green files/directories are tracked, red code is untracked.

Commit changes to the code

A commit contains a certain change, a message explaining the change, information about when the change occurred, and a unique ID. Please follow this style guide when commiting changes to this repository. It will make it easier for others to determine exactly what and why something was changed.

To commit all tracked changes, run

git commit

This will take you into a text editor. In the first line, make a header for the commit. If the change is complicated and requires more justification, go to a new line and elaborate there. After you are done, save the file.

A commit takes all tracked code and pushes that code into the local repository "head" (the place you are working right now). To upload your changes to GitHub, first make sure your local environment is up to date with the remote(online version):

git fetch
git rebase origin/<branch>

Then push your commit to the remote (online version) of the branch you are working on:

git push
⚠️ **GitHub.com Fallback** ⚠️