Learning Git

Josiah Baker & Steve Mortimer
November 1, 2016

Installing git

  • Instructions for installing on all platforms (Linux, Mac, Windows) are available here:
    https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
  • You can verify that your installation worked by checking the version of Git from the command line.
    • Mac Users: Open up your terminal
    • Windows Users: Open up Git Bash
    • Run the following command:
git --version

Generating Your SSH Key

  • Check for existing keys
    ls -al ~/.ssh
  • If you don’t have keys, then generate a key
    ssh-keygen -t rsa -b 4096 -C “YourEmail@mail.com”
  • Start the key agent
    eval “$(ssh-agent -s)”
  • Add the key to the agent
    ssh-add ~/.ssh/id_rsa

Copy Public Half of SSH Key to GitHub

  • Log into GitHub -> Settings -> SSH and GPG keys
  • Copy the key to clipboard and Paste
    • Mac Users
      pbcopy < ~/.ssh/id_rsa.pub
    • Windows Users
      cat ~/.ssh/id_rsa.pub > /dev/clipboard

Create your own repo on Github

  • Click on button to create new repository  
  • Repositories are a folder
    Create a folder with same name as new repo
    mkdir ~/members
    cd members
  • Copy/Paste the GitHub commands
    echo “# test” >> README.md
    git init
    git add README.md
    git commit -m “first commit”
    git remote add origin git@github.com:{UserName}/members.git
    git push -u origin master

Create your own repo on Github (cont.)

  • The first command initializes the repository
    git init
  • The second command tells git to track an individual file
    git add README.md
  • This commits changes on a tracked file
    git commit -m “first commit”
  • Now we tell git where our remote repository is
    git remote add {name} git@github.com:{UserName}/members.git
  • Save your local commited changes to the remote repository
    git push -u {name} {branch name}

Making a Commit (Adding to README)

  • Add your Name to the README file as a bullet
  • Save your changes
  • “Stage” all files, commit, then push to GitHub
    git add .
    git commit -m “Update README” -m “Add Change”
    git push origin master

Clone a Repo

  • Navigate to where you want repo to be
    mkdir ~/my-repos
    cd ~/my-repos
  • Copy/Paste the GitHub commands
    git clone git@github.com:NorfolkDataSci/members.git
  • Check that a folder with same name as the repo was created in the current working directory

Fixing a Merge Conflict

  • When 2 people change the same lines of code it creates a “merge conflict”
  • Merge conflicts need to be “resolved”, meaning reconcile the differences between commits
  • The first sign is not being able to push your recent commits

Fixing a Merge Conflict (cont.)

  • So you follow the hint and pull
    git pull
  • Then you receive a message about a “CONFLICT”
    (fix conflicts, then commit)

Fixing a Merge Conflict (cont.)

  • Open up the conflicted file (in this case README.md)
  • You will notice weird markings
    • The marker showing the start of the conflict
      “<<<<<<< HEAD"
    • The marker showing the end of your change (HEAD)
      ”=======“
    • The marker showing the end of the server’s version
      ”>>>>>>> 7bd926b……“

Fixing a Merge Conflict (cont.)

  • Open up the conflicted file (in this case README.md)
  • Edit file to how you want it. Make sure to remove weird markings
  • Commit like any other change and you’re done (Resolved!)
    git add .
    git commit -m “Fix merge conflict”
    git push

Fixing or Resetting Work

  • It’s possible to erase commits back to a certain point
    • Click on the commits button to view  
    • Click copy button to get commit hash on clipboard
  • Run the following command to reset your local commit history
    git reset ––hard 2feeb51899f80a2ddea5b353260841350285406f
  • Commit like any other change, but add “-f” on your git push
    git push -f

Working on Branches + a Pull Request

  • Start work on a “branch”. It’s a copy of your current state
    git checkout -b my_branch
  • This branch is only local, so push it to GitHub
    git push -u origin my_branch
  • Make a change to the README.md file, commit, & push
  • Next switch back to master branch and look at README.md
  • git checkout master
  • Your change is gone! It only exists on “my_branch”. Switch back now
  • git checkout my_branch

Branching + Pull Request

  • Branches isolate code until ready to be “merged”
    Create a pull request when you are ready to merge
    • Look in GitHub for “my_branch”
    • Click the button that says “Compare & pull request”
  • Pick a “target” branch (where you want new code to be applied)
  • Review that your changes make sense
  • Add a message outlining why this pull request is needed and how it essentially solves the problem of why it was needed

Items Still Not Covered

  • Stashing work with git stash and git stash pop
  • More advanced branching with git checkout
  • Forking and staying current with git rebase
  • Closing issues via commit messages (more info here)
  • Other ways to fix or undo your git history (more info here)
  • Git LFS (Large File Store) for versioning large data files