Git Cheat Sheet
This is probably the most important page of this course, as it contains a summary of all the commands you should know to use git and github.
- git init : Tell git to start version controlling the files in a directory (initialises git in a directory)
- git status : Tell git to print the status of the files in the version controlled directory.
- git add : Tell git to start monitoring (tracking) the versions of a new file, e.g.
git add README.mdwill tell git to trackREADME.md
- git commit -a : Tell git to save a new snapshot version of all of the tracked files in the directory. The
-ameans “all files”. You can commit new versions of individual files if you want, but this is not recommended. - git diff : Tell git to show the differences between the files in the working directory and the last saved version in the git repository. This will show the differences for all tracked files. Use
git diff FILENAMEto limit to only the fileFILENAME
- git checkout VERSION FILENAME : Tell git to bring
VERSIONversion ofFILENAMEinto the current working directory. IfVERSIONismainthen restore the last version ofFILENAMEthat was saved to the repository.
git checkout VERSION : Tell git to change the working directory back to a specificVERSIONnumber. IfVERSIONismain, then return the working directory to the last saved version in the repository. - git log : Print a log of the versions in the repository. Use
git log -n Nto limit to the lastNversions. You may need to useqto exit from the text viewer if there are a lot of versions to print. - git mv OLD NEW : Rename a file from name
OLDto nameNEW. - git rm FILENAME : Remove the file
FILENAMEfrom the working directory (it still exists in the repository). Will only work if the file is tracked by git and doesn’t have any changes. Use-fto force removal of files. - git clone LINK : Clone a repository from a remote repository to your local computer.
- git push : Push versions that are saved in the local repository (.git folder) so they are backed up to a remote repository (.git folder)