Git and GitHub Basics



Git Configs

# Tell git how you are

git config --global jeansn

git config --global

git config

How Git works




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

Staging Files


#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 staged files
git commit -m "created git blog post"

# review commit history
git log

# condenced
git log --oneline

Undoing Things


# 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

# Reset commit  ( Remove all commits to a point in time, files become uncommited )
git reset 5bb549d



# 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



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

#Start new local repository
git init
git remote add origin
# Set a new remote

git remote -v
# Verify new remote
> origin (fetch)
> origin (push)

git pull origin master

Remote name already exists

Rename remote

git remote -v
# View existing remotes
> origin (fetch)
> origin (push)

git remote rename origin destination
# Change remote name from 'origin' to 'destination'

git remote -v
# Verify remote's new name
> destination (fetch)
> destination (push)

Delete remote

git remote -v
# View current remotes
> origin (fetch)
> origin (push)
> destination (fetch)
> destination (push)

git remote rm destination
# Remove remote
git remote -v
# Verify it's gone
> origin (fetch)
> origin (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!