Mohamed Abbas | Architect | Blogger | Trainer

SCM Git Advanced

SCM GIT ADVANCED CHEAT SHEET

GIT PULL

Command Description
git pull --rebase <remote> Fetch the remote’s copy of current branch and rebases it into the local copy. Uses git rebase instead of merge to integrate the branches.

GIT PUSH

Command Description
git push <remote> --force Forces the git push even if it results in a non-fast-forward merge. Do not use the --force flag unless you’re absolutely sure you know what you’re doing.
git push <remote> --all Push all of your local branches to the specified remote.
git push <remote> --tags Tags aren’t automatically pushed when you push a branch or use the --all flag. The --tags flag sends all of your local tags to the remote repo.

GIT RESET

Command Description
git reset Reset staging area to match most recent commit, but leave the working directory unchanged.
git reset --hard Reset staging area and working directory to match most recent commit and overwrites all changes in the working directory.
git reset <commit> Move the current branch tip backward to <commit>, reset the staging area to match, but leave the working directory alone.
git reset --hard <commit> Same as previous, but resets both the staging area & working directory to match. Deletes uncommitted changes, and all commits after <commit>.

GIT REBASE

Command Description
git rebase -i <base> Interactively rebase current branch onto . Launches editor to enter commands for how each commit will be transferred to the new base.

GIT STASH

Command Description
git stash This command saves your current changes and cleans your working directory, so you can safely switch branches. Later, when you want to get those changes back, just run
git stash pop This will bring back the saved work exactly where you left off. It is a great way to pause your work and resume it later without creating unnecessary commits.
git stash list List stack-order of stashed file changes.

GIT DIFF

Command Description
git diff HEAD Show difference between working directory and last commit.
git diff --cached Show difference between staged changes and last commit

REMOTE REPOSITORIES

Command Description
git remote add <name> <url> Create a new connection to a remote repo. After adding a remote, you can use <name> as a shortcut for <url> in other commands.
git fetch <remote> <branch> Fetches a specific <branch>, from the repo. Leave off <branch> to fetch all remote refs.
git pull <remote> Fetch the specified remote’s copy of current branch and immediately merge it into the local copy.
git push <remote> <branch> Push the branch to <remote>, along with necessary commits and objects. Creates named branch in the remote repo if it doesn’t exist.

GIT LOG

Command Description
git log -<limit> Limit number of commits by <limit>. E.g. ”git log -5” will limit to 5 commits.
git log --oneline Condense each commit to a single line.
git log -p Display the full diff of each commit.
git log --stat Include which files were altered and the relative number of lines that were added or deleted from each of them
git log --author= ”<pattern>”" Search for commits by a particular author.
git log --grep=”<pattern>” Search for commits with a commit message that matches <pattern>.
git log <since>..<until> Show commits that occur between <since> and <until>. Args can be a commit ID, branch name, HEAD, or any other kind of revision reference
git log -- <file> Only display commits that have the specified file
git log --graph --decorate --graph flag draws a text based graph of commits on left side of commit msgs. --decorate adds names of branches or tags of commits shown.

GIT CHERRY PICK

Command Description
git cherry-pick This command will apply the changes from the specific commit you reference (<commit-id>) to your current branch, helping you maintain focus on just the changes you need.