Git is a version control system (VCS) that tracks changes to projects.
GitHub is a web-based platform that uses Git and allows developers to store, share, and collaborate on code.
git status - Shows the state of your working directory and staging area. git diff - Shows the differences between two commits, branches, files, or the working directory and the staging area. git log - Shows the history of commits in the current branch, along with their messages, authors, and dates. git status # Shows the current state of the working directory and staging area (modified, staged, untracked files)
git diff file_name # Shows the changes in that file compared to the last committed version (or between working directory and staged version)
git log # Shows the commit history for the repository
git init - Creates or initializes a new local Git repository in the current directory. cd repository
git init
# Initialized empty Git repository in...
ls -a
# You should see the .git folder
# If the repository is not connected to GitHub
cd new_repo
ls -a
# You should see the .git folder
rm -rf .git
# If the repository is connected to GitHub
cd new_repo
ls -a
# You should see the .git folder
git remote -v
# You should see a connection
rm -rf .git
git remote -v
# You should no longer see a connection
# You will need to delete the repository on GitHub.
git add - Adds changes in your working directory to the staging area, which is the temporary area where you can prepare your next commit. git commit - Records the changes in the staging area as a new snapshot in the local repository, along with a message describing the changes. git push - Uploads the local changes to the remote repository usually on a platform like GitHub or GitLab. git clone - Copies an existing remote repository to your local machine. cd repository
git add .
git commit -m "Initial commit"
git remote add origin git@github.com:grantdavis303/[name].git
git branch -M main
git push -u origin main
Forking a repository means creating your own copy on GitHub.
Cloning a repository means pulling down that repository to your own local machine so you can edit the files and push up any changes to the cloud.
git clone # Paste SSH code
git remote -v
origin git@github.com:grantdavis303/.....git (fetch)
origin git@github.com:grantdavis303/.....git (fetch)
git checkout - Switches your working directory to a different branch or commit, discarding any uncommitted changes. git branch - Lists, creates, renames, or deletes branches in your local repository. A branch is a pointer to a specific commit.
git branch - See current branchgit branch --all - See all branchesgit pull - Downloads the latest commits from a remote repository and merges them with your local branch.
git pull --rebase - No merge commit. Moves your work to the top. Takes local commits and replays them on top of another branch. git pull origin -branch-name - Immediately merges the specified branch into your current branch. git merge - Creates merge commit. Combines histories. Combines the changes from one branch into another branch, creating a new commit so there are no conflicts. git fetch --all - Contacts all configured remotes, downloads new commits, branches, and tags. # Checking Out Main
git checkout main
git branch
# Checking Out a New Branch
git checkout -b [name of branch]
git branch
# Checking Out an Existing Branch (Locally)
git branch -a
git checkout [name of branch]
git branch
# Checking Out an Existing Branch (GitHub)
git branch -a
git fetch -a
git checkout [name of branch]
git pull
# Updating Main from GitHub
git checkout main
git pull
# Updating a Branch from Main
git pull origin main
# Updating a Branch from Another Branch
git pull origin [name of branch]
git merge
git fetch --all
git pull origin SCRUM-10/new-feature
git fetch --all
git pull origin SCRUM-10/new-feature
# Deleting a Branch
git branch -D [name of branch]
It is recommended to add commits on a feature basis, not a time basis. Add commits when a new piece of functionality has been added to the repository.
A good commit message is brief enough that it is easy to read, but detailed enough that it is easy to understand.
Tips for writing good commit messages:
# Adding Individual Files
git add [file name]
git commit -m "Adding a single file"
# Adding All Files
git add .
git commit -m "Adding all files"
# Pushing Commits on Main
git push origin main
# Pushing Commits on a Branch
git push origin [name of branch]
# Reverting to a Previous Commit (Soft)
git log
# Copy commit ID number
git reset --soft # Paste commit ID number
# Reverting to a Previous Commit (Hard)
git log
# Copy commit ID number
git reset --hard # Paste commit ID number
git push origin [name of branch]
.gitignore Files touch .gitignore
Inside the .gitignore file, copy and paste:
# GitIgnore
# List all files and directories you wish to ignore.
# See more here: https://git-scm.com/docs/gitignore
/.env
/.env.local
/.DS_Store
**/.DS_Store
If there are already files in repository, get rid of them with:
git rm -r --cached '*.DS_Store'
git commit -m "Remove DS_Store Files"
git push origin main