Git & Github

Version Control for coding. Here I will throw in commands I commonly use in my projects.

1. Starting a Project

Creating a New Project

  1. Create a new directory and navigate to it

mkdir my_project && cd my_project
  1. Initialise Git versioning for the project (this will create a .git directory)

git init
  1. Map the project to an existing remote Github repository

git remote add origin <[email protected]:<Github-Username>/<Github-Repository>.git>

Making Changes to an Existing Project as a Collaborator

  1. Download a copy of the repository locally on your computer. There are 3 main ways to do this:

    1.1. Via HTTPS (requires Github login/password)

     git clone https://github.com/<Github-Username>/<Github-Repository>.git

    1.2. Via SSH (requires ssh key)

     git clone [email protected]:<Github-Username>/<Github-Repository>.git

    1.3. Via Github CLI

     gh repo clone <Github-Username>/<Github-Repository>

2. Creating Local Changes

  1. Shows list of changed files

  1. Shows differences in changed files. Note adding --staged shows differences in staged (after git add) files

  1. Shows the commit history

  1. Stage (add) files to be committed. . will add both tracked and untracked files, -u will add only tracked files. Source

  1. Commit your added files

If you made a mistake, you can use git reset to unstage changes.

Adding a filepath argument will only unstage that particular file

If you want to completely discard all changes and reset to the last commit you can either

3. Publishing your Local Changes

After committing changes from section 2, your changes will only be reflected in your local computer. If you want to push those changes to the online public Github repository so your team can use your changes, you have to run

4. Updating the Source Code

If a team member has pushed changes and you want to update your code, then you can choose to:

  1. Download the updated changes (without modifying your current local code)

  2. Download the updated changes and updates your current local branch code with the new \ code

If you made current local changes and still want to pull in the new code, you can either choose to commit the changes (Section 2), or stash them. Stashing will keep a copy of your code and reset your code to the last local commit (so you can pull in the new code without conflicts)

If you want to retrieve the stashed code, you can

To find what exists in the stash, you can run

To empty the stash, you can run

5. Branching

Work in branches to develop a feature, then merge with master via a pull request

To list all local branches:

To delete (force) a branch, add the -D flag

To create and navigate to a new local branch

6. Other Useful Commands

If you have updated .gitignore to ignore already tracked files, you can type the following to stop tracking the file.

7. Setting up Git/Github on a New Computer

  1. Install Git

  2. Configure Git

You can view your current configuration by

You can update your email and name (for your commits) by:

  1. Add an SSH Key for more convenient authentication.

  2. Generate a GPG Key for encrypted and signed commits.

Last updated

Was this helpful?