Skip to content

Latest commit

 

History

History
66 lines (54 loc) · 3.88 KB

Git Cheatsheet.md

File metadata and controls

66 lines (54 loc) · 3.88 KB

Git Cheatsheet

Initializing and Removing Git Repositories

git init : Initialize a repository.
rm -rf .git : Removes Git version from the folder.

git status : Checks the status of the git repository.

This provides information on:

  • Where the HEAD is pointing.
  • Files that have been changed but not committed.
  • Files that are in the staging area.
  • Outlines any merge conflicts and point to files that are the issue.

Adding

git add [name] : Adds specified file to the staging area.

Multiple files can be chained together : git add [name] [name] [name]
git add . : This will add all untracked files and any files that have been changed to the staging area.

Deleting

git rm [name] : Removes specified file from the repository.
git rm -r [name] : Removes folder (including all file contained within) from the repository.
git rm -r * : Removes all files from repository.

Committing

git commit -m "message" : Commits the file from the staging area to the branch with a message outlining changes.

Needs to always include a message so that people can understand what has been done. git log : Provides user with history of commits.

Pushing to GitHub

git push -u origin master : This sets the upstream so that in future pushes to GitHub you can just use git push -u without having to type out "origin master" as this is now the default.
git push -u [remote name] [branch] : Pushes items in staging area (once committed) to GitHub on the specified [branch].

Pulling from Github

git clone [remote name] : Clones the specified GitHub repository to the local computer.
git pull [remote name] [branch] : Pulls the specified data from GitHub to the local repository.

Removing File from Staging Area

git reset [name] : Removes specified file from staging area.

Reverting to a Previous Point

git checkout -- [file] : Reverts file to a previous commit point.
git checkout [commit hash] : Reverts to a previous commit point.

Check git status and copy the commit hash.
git reset HEAD~1 : This will reset the HEAD to point one commit back.

Linkage to GitHub

git remote -v : Displays list of remote repositories.
git remote add origin [repo] : Links Git to Github.
git remote remove [repo] : Remotes the remote link to Github.

Branching

git branch : Shows current branchs available and branch that is being worked on currently.
git checkout [branch] : Moves to a specified branch.
git branch [branchname] : Creates a new branch from the Main(Master) branch.
git branch -d [branch] : Deletes specified branch.
git branch -D [branch] : Deletes branch (regardless of whether it has been merged).
git merge [branchname] : Merges the [branchname] with the main branch.
git merge master : Merges the master branch with your current branch.

This should be done regularly to stop the branch you are working on from getting too far behind the master.
This will highlight any conflicts between your current branch and what has been done to the master branch since you last pulled.
If using VS Code this can be done directly from the editor (it will highlight changes and buttons will appear at the top that you can click to accept/compare)
git checkout --orphan [branch] : Unlinks the specified branch from the current branch.

Excluding Files

touch .gitignore : This will create a .gitignore file.

For files that you do not want to have pushed to Git, you enter the file and list the file(inc. extension) that you want to exclude.