Git and Github Basics
A Git and Github cheat-sheet for quick reference to some basic commands. Includes a basic introduction to Git and Github with more information found here. Git and Github have a bit of a learning curve, but once your files are in GitHub, it's easy to share the repository with others.
What is Git
Git is a distributed version control system that tracks versions of files. It is often used to control source code by programmers who are developing software collaboratively. Git maintains a local copy of a repository, a.k.a. repo, with history and version-tracking abilities, independent of network access or a central server. A repo is stored on each computer in a standard directory with additional, hidden files to provide version control capabilities.
What is Github
GitHub is a cloud-based platform that allows developers to store, share, and collaborate on code. It uses Git software, providing the distributed version control of Git plus access control, bug tracking, software feature requests, task management, continuous integration, and wikis for every project. It is commonly used to host open source software development projects.
//CONFIGURATIONS
//First set some configuration settings to work with Git.
//If you’ve signed up to Github use those settings so that they can interact with each other.
git config --global user.name "Your Username"
//The username you use on Github
git config --global user.email "Your email"
//The email you use on Github
git config --global color.ui true
//highlights the git code for easier reading
git config --list
//Lists your git config settings
//global - default settings used in all git repros
//.DS_STORE FILES
//Finds and removes .DS_Store files from your project and Git
//Do this from the root of your project
find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch
**/.DS_Store //Add to the .gitignore file
//STARTING A REPOSITORY
//A Git repository is a folder that holds your project. It can be empty or already contain project files and folders. Git places a hidden folder at the root of the project folder/repository where it tracks the projects files and watches for changes.
git init
//initialize a repository
git status
//Shows the status of the repository
//STAGING FILES
//The command git status shows any changes to the projects files.
//Before Git commits the files it needs to add them to the staging area.
//Git manipulates the staging area with the following commands.
git add <file-name>
//Add changed or new file to commit stage
git add <file-name> <another-file-name> <and-another-file-name>
//Add multiple changed or new files to commit stage
git add .
//Add all changed or new files in the root directory - note the period
git add --all
//Add all changed or new files in the project
git add -A
//Add all changed or new files in the project
git rm --cached <file-name>
//Remove file from commit list
git reset <file-name>
//Remove file from commit list
git commit -m "your message"
//Commit with message
git reset --soft HEAD^
//Undo the last commit
git commit --amend -m "enter your message"
//Use instead of git reset to amend the last commit and rewriting the message
//example:
git add I-forgot-this-file
git commit --amend -m "added I-forgot-this-file"
//PULLING AND PUSHING FROM AND TO REPOSITORIES
//Working between the local and remote repositories(Local to Github)
git remote add origin <repo link>
//Set up the origin path to your on-line repository
git push -u origin master
//upload to the on-line repository master branch
git push
//because we ran git push -u origin master we can now just use git push
git clone <clone link>
//Download a on-line project to a folder on your computer
git clone <clone link> new-name
//Download and change the name if you wish
git pull
//Update the local repository with any changes made in the remote repository
//BRANCHING
//The main branch in a repository is the master branch.
//If we need to add new features to the project we can make them on separate branches so as not to upset the master branch.
//This allows multi developers to work on different features at the same time.
//When the features are finished and passed the branches can be merged with the master branch.
git branch
//Lists all the repository branches
git branch <branch-name>
//Create a new branch
git checkout <branch-name>
//Switch to branch
git merge <branch-name>
//Merge the branch into master branch
//Switch to the master branch before merging
git branch -d <branch-name>
//delete branch
//Switch to the master branch before deleting
git checkout -b <branch-name>
//Create and switch to new branch