GIT is a distributed version-control system for tracking changes in source code during software development, it is now the most popular version-control system.
There are 3 stages of a file
There are four areas of git
Below is a simplied diagram on how git works with the four areas metnioned above
Git Setup | git config --system --list # system variables (if blank git has not been setup) git config --global --list # user specific variables (if blank git has not been setup) git config --global --list # project specific variables git config --global --edit # edit the git global configuration file git config --global user.name "Paul Valle" git config --global user.email "paul.valle@datadisk.co.uk" git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' # multiInst -notabbar -nosession -noPlugin" git config --global color.ui true git config --global alias.changed 'show --pretty="format:" --name-only' # create an alias option to git git config --global mergetool.keepBackup false # prevent *.orig file being created during merge conflicts |
Help | git help [-g|-a] # -g = gives you the git guides, -a = subcommands git help [command|concept] # open help page in a browser |
Init | git init # initialize a local git repository |
Clone | git clone |
Status | git status # get git status about staged and commits |
Log | git log # bring back all logs git log --oneline # bring back all logs but summary on one line for each commmit git log --oneline --graph --decorate -all # bring back all logs but summary on one line for each commmit, # --graph creates GUI graph, --decorate print out ref names, --all include all branches git log --stat --oneline # bring back the logs and including number of changes/deletions/etc git log -n <number> # bring x number of last commits git log <file> # bring back logs of specific file git log <number> # bring back specific number of logs from top (latest commits) git log <since ID>..<until ID> # bring back specific logs between since and until commit ID's (exclusive of since part) git log --since 2017-01-27 # bring back all logs since specific date git log --author="Paul" # bring back all logs from specific author got log --grep="Initial" # bring back all logs with specific text in them git log HEAD # bring back all logs starting from the HEAD |
Blame | git blame <file> # who changed what and when on a specific file |
Add | git add <file1> <file2> .... # add specific files to staging area ready for commit to repository (basically track a file) git add . # add all files in current directory and below to the staging area ready for commit to repository |
Remove | git rm -f <file1> # remove file completely, -f will force the removal if the file if staged git rm --cached <file1> # remove file from the staging area, file is retained in directory but not under git control |
Rename | git mv <old_name> <new_name> # rename a file into the staging area ready for commit to repository git mv <file1> <dir>/<file1> # mv file to different location in staging directory ready for commit to repository |
Commit | git commit # commit the staged files using the default text editor git commit [-m|--message] "<message>" # commit staged files to repository git commit -am "<message>" # staged and commit all in one go, does not do delete or rename files, also does all files/dirs |
Difference | git diff # compare all changes between working directory and the staging area git diff <file> # compare specific file changes between working directory and the staging area git diff HEAD # compare all changes between working directory and the repository (committed) git diff HEAD <file> # compare specific file changes between working directory and the repository (committed) git diff [--staged|--cached] # compare all changes between the staged area and the repository (committed) git diff [--staged|--cached] [HEAD] # compare specific file changes between the staged area and the repository (committed) git diff <commit id> HEAD # compare the commit ID with the HEAD in the repository (committed) git diff HEAD^ HEAD # compare previous HEAD commit to the HEAD in the repository (committed) git diff HEAD~1 HEAD # same as above command git diff [HEAD^|HEAD~1] # compare previous HEAD commit with the working directory (notice no HEAD on end of line) git diff <branch> master # compare local specific branch with local master git diff master origin/master # compare local master branch with remote master branch git diff master origin/<branch> # compare local master branch with remote specific branch git diff-tree --no-commit-id --name-only -r <commit ID> # list only filenames in commit git diff-tree --no-commit-id --name-only -r <commit ID> # list only filenames in commit |
Show | git show --pretty="" --name-only <commit ID> # list only filenames in commit git show --stat --oneline <commit ID> # list filenames and change count in commit |
Whatchanged | git whatchanged <commit ID|HEAD> # very low level details of commits |
Branch | git branch [-a] # list all git branches, -a means all branches (local and remote) git branch <branch name> # create new branch (includes git master log) git branch -m <old name> <new name> # rename a branch git branch [-d|-D] <branch name> # delete a branch, -d normal delete, -D force delete |
Checkout | git checkout <branch name> # switch branch git checkout <commit ID> # checking out a specific commit (you can never modify the master, to # save any changes (because this is read-only) use git checkout -b) git checkout <commit ID> <file name> # checks out a previous version of the file, this can then be committed to overwrite # the file (does overwrite existing file) git checkout -b <branch name> # checkout to a new branch used in above scenario, you will also switch to it git checkout HEAD <file name> # revert the file to the HEAD commit |
Revert | git revert HEAD # restore to the previous commit, will undo last changes, but maintains previous commit history as it creates new commit |
Reset | git reset <file name> # unstages a file that has been added to git, does not save commit git history (dangerous), git reset # will unstage all files that have been added to git with changes, but the actual changes in the file itself will remain git reset --hard # will unstage all changes plus remove the actual changes from the files (destroys all uncommitted changes) git reset <commit ID> # will uncommit to commit id, all previous files that where changed into working directory (not staged or commited), # however the actual changes are still reflected in the files git reset --hard <commit ID> # will uncommit to commit id, all previous files that where changed into the working directory, however # all file changes will be removed as well (very dangerous) |
Clean | git clean [-f|-n|-d|-x] # remove untracked files or directories from working directory, -n performs a dry run first, -f force removal, # -x includes any files in .gitignore as well |
Merge | fast forward # If Master has not diverged (no changes), instead of creating a new commit, git will just point master to the latest commit of the feature branch, previous branch commits are lost no fast forward # If master has not diverage, a merge commit is created and the previous branch commits are preserved even if branch is deleted 3 way merge # Master has diveraged, so a merge commit will be used, previous branch commits are preserved even if branch was deleted (add *.orig to .gitignore file or use git config to stop *.orig files being created) git merge <branch to merge with> # merge a branch into current checked out branch (always do a diff first), a merge will also perform a commit git merge --abort # abort a merge when there have been conflicts |
Remote | git remote add origin https://github.com/<repo> # setup origin to point to github git remote set-url origin https://github.com/<repo> # change origin to point to github git remote set-url origin git@github.com:datadiskpfv/<repo> # change origin to use SSH to access github (must setup SSH keys) git remote -v # list remote repositories git remote show origin # detailed view of remote repository |
Origin | git origin |
Pull | git pull origin master # pull lastest commits from remote repository like GitHub git push origin master # push lastest commits to remote repository like GitHub |
Fetch | git fetch |