Mohamed Abbas | Architect | Blogger | Trainer

SCM Git Basics

SCM GIT Basics Tutorial

GIT BASICS

Command Description
git init <directory> Create empty Git repo in specified directory. Run with no arguments to initialize the current directory as a Git repository.
git clone <repo> Clone repo located at <repo> onto local machine. Original repo can be on the local filesystem or a remote machine via HTTP or SSH.
git config user.name <name> Define author name for all commits in current repo. Use --global to set it for all repos for the current user.
git add <directory> Stage all changes in <directory> for the next commit. Replace <directory> with a file to stage a specific file.
git commit -m "<message>" Commit the staged snapshot with a message instead of opening the default editor.
git status List which files are staged, unstaged, and untracked.
git log Display the entire commit history using the default format. Use options to customize the output.
git diff Show unstaged changes between your working directory and the index (staging area).

UNDOING CHANGES

Command Description
git revert <commit> Create new commit that undoes all of the changes made in <commit>, then apply it to the current branch.
git reset <file> Remove <file> from the staging area, but leave the working directory unchanged. This unstages a file without overwriting any changes.
git clean -n Shows which files would be removed from working directory. Use the -f flag in place of the -n flag to execute the clean.

REWRITING GIT HISTORY

Command Description
git commit --amend Replace the last commit with the staged changes and last commit combined. Use with nothing staged to edit the last commit’s message.
git rebase <base> Rebase the current branch onto <base>. <base> can be a commit ID, branch name, a tag, or a relative reference to HEAD.
git reflog Show a log of changes to the local repository’s HEAD. Add --relative-date flag to show date info or --all to show all refs.

GIT BRANCHES

Command Description
git branch List all of the branches in your repo. Add a <branch> argument to create a new branch with that name.
git checkout -b <branch> Create and check out a new branch named <branch>. Drop the -b flag to checkout an existing branch.
git merge <branch> Merge <branch> into the current branch.

GIT CONFIG

Command Description
git config --global user.name <name> Define the author name to be used for all commits by the current user.
git config --global user.email <email> Define the author email to be used for all commits by the current user.
git config --global alias.<alias-name> <git-command> Create shortcut for a Git command. E.g., alias.glog "log --graph --oneline" makes git glog run git log --graph --oneline.
git config --system core.editor <editor> Set text editor used by commands for all users on the machine. Use your editor’s command (e.g., vi).
git config --global --edit Open the global configuration file in a text editor for manual editing.