Thursday, February 25, 2021

Useful Git Commands

We went through git in class today. We covered basic commands and terminology. I'll review some of those commands in this post as well as some extra commands that you might need when working on projects.

How do I connect git to GitHub?

  1. Make a new directory somewhere locally

    mkdir new-project
    
  2. cd into that directory and run a git init

  3. Add files and make code changes, these can be very basic for now

  4. Add and commit these changes

    git add . 
    # or 
    git add <filename>
    
    git commit -m "message"
    # careful that you use only single or double quotes
    
  5. Change the default branch to main with git branch -M main

  6. Login to GitHub, create a new repo with the same name as your local project

  7. Add the remote with git remote add origin <remote-name>

  8. Push to GitHub with git push -u origin main

  9. From here on out you can make changes, commit and push as your please

How do I use git diff in VSCode?

I find the output of git diff difficult to sometimes parse therefore I opt to use the GUI equivalent of the command in VSCode. To access this command:

  1. Open your project in VSCode

  2. Navigate to View (it's in the menu bar) and click on SCM

  3. Click on the file you wish to see the git diff of

How do I delete files from a git repository?

You might accidentally add a file to your source control (like a big video or image file) and push it to GitHub. To remove it from both git and GitHub you'll need to:

  1. Delete the file from git

    git rm --cached <filename>
    
  2. Do a force push to GitHub

    git push origin main --force
    

How do I revert back to a previous commit?

There are many different approaches to revert back to previous commits. The approach that I like to take is as follows:

  1. Run a git log and copy the commit id of the commit you wish to roll back to, for example the commit id for this commit

    commit 5792c92bbb0c026f46c2c6e8a993ec5c603f1165
    Author: Harrison Malone <[email protected]>
    Date: Tue Feb 9 11:05:34 2021 +1100
      once again changed style and added privacy policy
    

    is <code style="word-break: break-word;">5792c92bbb0c026f46c2c6e8a993ec5c603f1165</code>.

  2. Checkout to that commit and make a new branch

    git checkout -b <branch-name> <commit-id>
    
  3. In this branch you can do whatever it is you need to do, change your code, more commits etc

  4. When you're happy with your code in this branch you can do a couple of things, if you wish to add the branch code back to main you could

    git checkout main
    git merge <branch-name>
    
  5. You may get some conflicts when you run this command, resolve these conflicts in VSCode by clicking on Accept Incoming Change

  6. You'll then need to add and commit the conflict resolution to the main branch

Why did we do the SSH config?

We followed the GitHub SSH guide to ensure that we never have to enter our password over and over when connecting to GitHub. You'll be doing this potentially hundreds of times a day as a developer so this saves us a lot of time.

How do I exit out of vim?

vim is a command line text editor that ships default with macOS and some Linux distributions. We aren't going to teach you how to use vim but you might inadvertently find yourself in vim. For example if you try to make a commit without the -m and ""

git commit

you'll be thrown into vim. To exit out you vim you need to type :q.