Categories
Git GitHub

Git & GitHub Basics: What I Learned from “Git Going Fast: One Hour Git Crash Course” on Udemy

I just completed the very short Udemy course, Git Going Fast: One House Git Crash Course, which taught the basics of Git and connecting with GitHub. This is what I learned…

To be completely honest, I went into this course expecting it to cover many more commands and features of Git and GitHub, but on reflection it was just over an hour long and was designed to start from scratch so that was an unrealistic expectation. That being said, it solidified my understanding of some of the basics of Git and will serve as a solid foundation for taking a more advanced course at a later date.

Getting Help with Commands

If you ever need to find a command or need to know which arguments (or options) it can take, you could just Google it. Alternatively, you could ask Git directly like so:

# Shows help for the config command
git help config

Initial Configuration

To get started, you need to configure your name and email address at a minimum. This essentially sets you up as a user.

# Configure your name
git config --global user.name "Your Name Here"

# Configure your email address
git config --global user.email "email@address.com"

# See a list of users
git config --global --list

Initialising a Repository

Simply use the git init command to initialise a new local repository, whether that be for a new or an existing project.

# Create a new project
git init my-new-project

# Start with an existing project
cd my-existing-project
git init

Adding (Staging) & Merging Commits

Whenever you make a change to a file – such as editing it, moving it or removing it – you will need to stage the change before committing it. You can stage changes in a couple of ways.

# Stages single changed file
git add index.html

# Stages all changed files
git add .

Once you’ve staged all of your changes, it’s time to commit them.

# Commits staged changes
git commit -m "This is the commit message"

You can do both the staging and the merging in the same command if you prefer to.

# Stages and commits all changes
git commit -am "This stages and commits all changes"

Unstaging & Removing Changes

To unstage a change, use the git reset command.

# Unstages the index.html file
git reset HEAD index.html

To remove a change – reverting the changed file back to its original state – use the following command.

# Reverts any unstaged changes made to index.html
git checkout -- index.html

Showing Commit History / Logs

If you don’t need or want to use a GUI tool, such as TortoiseGit, to review your repositories log, you can use the command line instead.

# Show logs
git log

# Show cleaner logs
git log --online --graph --decorate --color

Moving & Removing Files

Sure, you can use Windows File Explorer or even Visual Studio Code to move a file to a different folder, but you can also use the command line.

# Moves index.html file to /destination-dir
git mv index.html destination-dir

The same goes for removing files, too.

# Removes the index.html file
git rm index.html

Ignoring Files

To ignore certain files and directories from being version controlled and keep them off of GitHub – such as log files, credentials and the node_packages directory – create a .gitignore file in the root of your repository directory and enter the name of the things you want Git to ignore.

# Ignores all log files
*.log

Don’t forget that you’ll need to stage and commit the .gitignore file as it’s part of the project.

Setting up SSH & Connecting with GitHub

In order to connect to your GitHub account and start working with remote repositories on there, you’ll need to first setup an SSH connection.

# Changes directory to your root, typically /Users/username
cd ~

# Creates the .ssh directory (only if you don't have one)
mkdir .ssh

# Changes to .ssh directory
cd .ssh

# Generates SSH keys
ssh-keygen -t rsa -C "email@address.com"

If all went well, you should be asked to confirm the location to save the key file. Follow these steps:

  • Saving the key to the default location should be fine
  • Enter a passphrase
  • Copy the contents of the id_rsa.pub file
  • Sig in to GitHub and go to Account Settings > SSH Keys
  • Click ‘Add SSH Key’
  • Give it a name (your computer), paste in the key and save it

To test the connection, go back to your terminal and try the following.

# Permanently adds GitHub to known hosts file
ssh -T git@github.com

Once you’re ready, click ‘New repository’ to create a new repository.

Give it a name and click ‘Create’.

Select the SSH option and go down to where it says “Push from existing repository”. Copy the first command from there. It will look something like this.

# Adds the remote GitHub repository as the origin
git remote add origin git@github.com:robdcal/git-demo.git

# Displays URLs of your remotes
git remote -v

The origin is the main repository on the remote side of things.

Pushing & Pulling Changes

Now that the connection has been established, you can pull changes from the remote repository (if you have multiple developers working from the same project, or you work on the same project from different devices) and push changes to GitHub, too.

# Pull the latest changes from the remote origin (GitHub)
git pull origin master

# Push your changes to the remote, use -u in your first push
git push -u origin master

# You can leave -u out of further pushes
git push origin master

Leave a Reply

Your email address will not be published.