Skip to main content

Git and GitHub Basics


whyIusegit

GitHub

Git Configs#

# Tell git how you are
git config --global user.name jeansn
git config --global user.email jeansn123@gmail.com
git config user.name

How Git works#

repo

commits

commits

Create a New Git Repository#

This creates a .git folder in the root directory that tracks all the changes to the files.

git init

To exclude any files from being tracked you need to add a .gitinore file in the root of you project and add the files or folders that should be excluded.

# In the file you can add thing like this
node_modules
dist
.cache

Staging Files#

staging1

#Check which files are in the staging area
git status
# staged files will be in green
# Add file to staging
git add <path to file> # git add src\assets\images\Git\github.jpg
# remove file from staging
git rm --cache <path to file> # git rm --cache src/assets/images/Git/github.jpg
# Add all files to staging at once
git add .

Commiting Files#

commiting

# Commiting staged files
git commit -m "created git blog post"
# review commit history
git log
# condenced
git log --oneline

Undoing Things#

undoingThings

# Checkout commit ( Shows the code at a point in time )
git log --oneline
git checkout 5bb549d
# go back to current
git checkout master
# Revert commit ( Undo commit )
git revert 5bb549d
# to get out of the file
:wq
# Reset commit ( Remove all commits to a point in time, files become uncommited )
git reset 5bb549d

Branches#

branches

# create a branch
git branch <name> # git branch feature-1
# list branches
git branch -a
# switch to different branch
git checkout <branch name> # git checkout feature-1
# delete a branch
git branch -D <branch name> # git branch -D feature-1
# create branch and switch to it
git checkout -b <branch name> # git checkout -b feature-1

Merging Branches#

# first check out the branch where you are merging into
git checkout master
git merge feature-1

GitHub#

github2

Add remote branch in VSCode ( when remote repository already exists )#

#Start new local repository
git init
git remote add origin https://github.com/user/repo.git
# Set a new remote
git remote -v
# Verify new remote
> origin https://github.com/user/repo.git (fetch)
> origin https://github.com/user/repo.git (push)
git pull origin master

Remote name already exists

Rename remote#

git remote -v
# View existing remotes
> origin https://github.com/OWNER/REPOSITORY.git (fetch)
> origin https://github.com/OWNER/REPOSITORY.git (push)
git remote rename origin destination
# Change remote name from 'origin' to 'destination'
git remote -v
# Verify remote's new name
> destination https://github.com/OWNER/REPOSITORY.git (fetch)
> destination https://github.com/OWNER/REPOSITORY.git (push)

Delete remote#

git remote -v
# View current remotes
> origin https://github.com/OWNER/REPOSITORY.git (fetch)
> origin https://github.com/OWNER/REPOSITORY.git (push)
> destination https://github.com/FORKER/REPOSITORY.git (fetch)
> destination https://github.com/FORKER/REPOSITORY.git (push)
git remote rm destination
# Remove remote
git remote -v
# Verify it's gone
> origin https://github.com/OWNER/REPOSITORY.git (fetch)
> origin https://github.com/OWNER/REPOSITORY.git (push)

Clone a Repo#

git clone <repo link>

Push Code to Repo#

git push origin master

Pull Code Changes from Remote Repo#

git pull origin master

Thanks for reading!

Last updated on