This is a CheatSheet (or “How to…?”) that I wrote after taking the Udacity course : “How to Use Git and GitHub?”(link). The course is well built and it gives a solid overview of basic use of Git Version Control System, and in particular GitHub.
The course also includes 2 useful videos on how to configure your workspace.
1. Some definitions
repository: a collection of files and folders
commit: a user created checkpoint. They are ordered by date
$gitclonerepo_url{Copyentirerepositoryfromremotetolocal}$gitcheckoutcommit_id{Restoringapreviouscommit}{Givesawarning:"you are in detached `HEAD` state".Youcandetachthe"HEAD"byswitchingtoapreviouscommit.}$gitbranchnew_branch_name{Createanewbranch}$gitremote{Checkremoterepository}$gitremoteaddoriginurl{Addtheremoterepository}$gitadd*{Addtothestagingareaallfilesandfoldersinworkingdir}$gitfetch
4. How to …
4.1. commit (locally)
add files to staging area → git add file1 or git add *
check what’s in the staging area → git status
commit with message → git commit -m "commit_message"
The message must be less than 72 chars, explain problems solved and why the change
check status → git status
Sometimes you might have to force add with: “git add -f *”
4.2. Merge/Combine 2 branches
check active branch label → git branch
merge → git merge master branch_name1
commit → git commit
show commit merge → git log
delete label → git branch -d branch_name1
4.3. Initialize a (local) new repository
Go to the local folder
creates .git folder where all repo info are stored → git init
how which files has changed since last commit → git status (s)
4.4. Push repositories (local to remote/Github)
Create empty repository on Github (Do not initialize with README if the local repository has commits)
Go to the working local directory
check remote repository → git remote
add the remote repository → git remote add origin remote_url
show url for push/pull → git remote -v
Push the master branch (starting from the tip) → git push remote_name local_branch
(remote_name = origin and local_branch=master)
Commits that are already on remote are not pushed.
The branch name will be unchanged on the remote
4.5. pull/Update repositories (remote/Github to local)
git pull remote_name branch_name
Typically, remote_name=origin and branch_name=master
4.6. Fork a repository (copy the repo of another user to your Github account)