CS-1: GiT useful commands

What is a Git ? The Git is an open-source distributed version control system. It is very helpful for tracking changes in any files set. The cheat-sheet is a quick reference to the most used command-line commands separated into the sections.

configuration

: global name attached with the commit
$ git config —global user.name “<custom_name>”
: global email attached with the commit
$ git config —global user.email “<custom_email>”
: enables colors in command-line output
$ git config —global color.ui auto

repositories

: create a new local repository with the desired name
$ git init <repository_name>
: clone/download repository with full history from desired url
$ git clone <repository_url>

files modification

: list all modified files
$ git status
: show not staged file differences
$ git diff
: add file snapshot to the version control
$ diff add <custom_file>
: reset file changes
$ git reset <file_name>
: remove/delete the file from work directory and stages
$ git rm <file_name>
: remove/delete the file from the version control but keeps local
$ git rm —cached <file_name>
: move the desired file to new location and keep it for commit
$ git mv <source_file_name> <target_file_name>
: commit the file snapshot to the versioning history
$ git commit -m “<custom_commit_message>”

branch modification

: list all local branches for the current repository
$ git branch
: create a new branch
$ git branch <branch_name>
: switch to the desired branch and update the work files
$ git checkout <brach_name>
: merge the desired branch into the active current branch
$ git merge <branch_name>
: download all history from the repository, not merge with current branch
$ git fetch
: download history from the <bookmark>
$ git fetch <bookmark>
: merge changes from the <branch_name>, <bookmark> to the local
$ git merge <bookmark>/<branch_name>
: upload all local commits to the source
$ git push
: download history for the project and active branch
$ git pull
: remove/delete desired branch
$ git branch -d <branch_name>

suppress tracking

: edit repository file .gitignore to suppress versioning control for desired files/folders
*.iml
*.txt
build
: list all ignored elements in the project
$ git ls-files —other —ignored —exclude-standard

stash incomplete changes

:temporary save all modified tracked files
$ git stash
: restore most recently stashed files
$ git stash pop
: list all stashed changes
$ git stash list
: discard the most recently stashed changes
$ git stash drop

history

: list version history for the active branch
$ git log
: list version history for the active branch including renames
$ git log —follows <file_name>
: review differences between 2 branches
$ git diff <branch_first>…<branch_second>
: review metadata and content of the specific commit
$ git show <commit>

redo commit

: undoes all commits after the <commit>, keep local changes
$ git reset <commit>
: discard all history and changes back to specific commit
$ git reset —hard <commit>