Git



Git is a version control system.

  • What’s that? A version control system records changes to a file.
  • If someone changes a file (i.e. writes to it or deletes from it), git records the differences between the new version and the old version, and maintains a history. Multiple people can collaborate on a project and each person’s changes are documented. Fosters collaboration! Documentation

Installation

Before installing check to see whether you have git:

git --version

To install:

brew install git

Need to Know

  • MacOS comes with git preinstalled in /usr/bin/git
  • How to work with others?
    • Git allows you to create branches.
    • Branches can serve as separate workflows out of the same parent version which you can then merge back with main to update the parent code.
  • Found an error? Fantastic. Git lets you revert back to a previous version.
  • Git Cheat Sheet

Necessary configuration:

  • User name and emails can be set for a single repository and globally.

    git config --global user.name "Name"
    git config --global user.email email@email.com
    

Initial Set-Up

Either pull/clone a remote repository existing on Git to your local machine OR take an existing directory on your local and initialize it with Git.

To clone an existing remote repository in the current directory:

git clone https://github.com/user-name/repo-name.git

To initialize a local repository:

  1. Configure the directory to be a .git directory:

    git init
    
  2. Add a README file to the files currently tracked by git:

    git add README.md
    
  3. Commit the added files so they are now tracked:

    git commit -m "first commit"
    
  4. Rename the branch to main:

    git branch -M main
    
  5. Add the remote (i.e. Github) tracking:

    git remote add origin https://github.com/user-name/testrepo.git
    
  6. Push the staged commit to the remote branch main (-u flag is set-upstream)

    git push -u origin main
    

Bread and Butter Commands

  • To see all modified files and their status (changed, addded, deleted, commited):

    git status
    
  • To pull the remote repository down to local machine:

    git pull <remote repository>
    
  • To add specific files/directories to be commited.

    git add <files>
    
  • To add all the files to be commited.

    git add .
    
  • To stage your changes before publishing them:

    • The -m flag adds a message describing the changes.

    • These messages are permanent and will be kept in the history of your repository forever!

      git commit -m "<message>"
      
  • To publish your changes to your remote repository for others to use, see, and change:

    git push <remote repository>
    
  • To create a new branch and move into it:

    • -b flag creates the branch.

      git checkout -b <branch name>
      git checkout <branch name>
      
  • To list all branches:

    git branch -a
    

Best practices

  • Pull Before Push Always pull from the branch you are working on to make sure that you have the most recent version of the remote (i.e., Github) repository.
    • This lets you deal with merge conflicts locally on your machine rather than in your remote repository.
  • Push Only Working Code (POWC) Only push functional code to your remote repository so that when others checkout your branch, the code works.
    • Main and develop should generally not be pushed directly to.
    • Merge your code into a develop branch instead and have someone else ensure it works before changing the parent code (main).