The Basics of Git and GitHub - A Simple Cheatsheet

·

5 min read

The Basics of Git and GitHub - A Simple Cheatsheet

Photo by Yancy Min on Unsplash

A developer's workflow can be greatly enhanced with a version control system like git. A version control system provides a detailed history of file changes and, more importantly, makes it possible to revert to previous versions if necessary. Many developers opt to use third-party web services like GitHub to host their Git repositories. To initialize a repo, you first need to create a directory for where your project will live. Once you've done that, you can open up your terminal and change into that directory. Then, you can run the command git init, which will initialize an empty repo. You can then start adding files to this repo and committing changes.

Setting up user and email on Git

You can setup the username and email by running these codes: git config --global user.name “Username” then press enter. Again type git config --global user.email “user@usermail.com” and press enter again. Now type git config --list to show the configuration and verify that you have entered the correct name and email

git config --global user.name “Username”
git config --global user.email “user@usermail.com”
git config --list

Initialize an empty repository (repo)

To initialize a repo, you must create a directory where your project will live. Once you've done that, you can open up your terminal and change into that directory. Then, you can run the command git init, which will initialize an empty repo. You can then start adding files to this repo and committing changes.

git init

Adding or staging file on your repo with Git add filename.extension

To add a file to your repository, you first need to tell git which files you want to track. This is done with the git add command. Simply type git add followed by the file name you wish to track. For example, if you wanted to track a file named test.js, you would type git add test.js. If this is the only file in your repo, then git will now track it. If you have more than one file in your repo, then git will prompt you to select which files to track, or you can use git add . to stage all the files.

git add test.js
git add .

Commit your changes into a repo

To commit your changes, you'll need to stage them by running git add . Once your changes are staged, you can commit them by running git commit -m "Your commit message here". If you forget to add a message, you can always run git commit --amend to add one. To make an interactive commit with the ability to edit the message, run git commit -a instead.

git commit -m "Your commit message here"
git commit --amend
git commit -a

List changes using git status

If you want to check the status of your repository and see which files have changed, you can use the git status command. This will give you a list of all the files that have been modified since the last commit. You can also use the git diff command to see what changes have been made to a file since the last commit. You can also use the git diff --staged command to see what changes have been made to a file in staging.

git status
git diff
git diff --staged

Push change from local directory to GitHub using git push origin master Section:

To push changes from your local directory to GitHub, you first need to initialize a git repository. This can be done by running the git init command. Once you have initialized a repository, you can add files to it using the git add command. Finally, you can push your changes to GitHub using the git push origin master command. Note: you must connect your local git to GitHub using the ssh key or token method.

git push origin master
git push -u origin master

Get changes from GitHub into the local directory using git pull origin master

If there are any changes on the remote repository that you don't have locally, git pull will fetch them and merge them into your local copy. For example, if someone commits a change to a project branch on GitHub before you've updated your local branch with a commit from that branch, when you run git pull origin master, it will tell you what is different between your two branches. Once this is done, it'll tell you if anything was done since the last time you ran git pull.

git pull origin master

Deleting Files In Git

we use the command: git rm filename.extension To delete files using git

git rm filename.extension

Renaming Files In Git

we use the command: git mv filename.extension To rename the files using git

git mv filename.extension

Untracking Tracked Files:

we use the git rm --cached file.extension command. it will become an untracked file.

git rm --cached file.extension

Git Log

we need to type git log. After typing this, you can see the commits made on the repo. To exit, we need to type q on our keyboard and press enter.

git log

Unstaging

To unstage a file, use git restore --staged file.ext. It will unstage the file, and you can verify it using git status.

git restore --staged file.ext

Creating & Switching Branches In Git

create a branch by typing git checkout -b branchname. git checkout branchname we will switch from the master branch to your newly created branch. git checkout master then you will see that the files on your repo will revert to the master branch.

git checkout -b branchname
git checkout branchname
git checkout master