Most useful git commands by git stages

Most useful git commands by git stages

How to understand Git commands based on which stage it's used

In this post, my goal is to try to explain the git stages and the Git commands grouped by those divisions. Perhaps, at least it might be easier to understand a specific command based where it is used.

git-stages.png

Introduction

I've seem some people getting confused using git commands due to the lack of understating the 3 stages of Git.

1 - Working directory

The actual file's states in the file system, they can be tracked or untracked, they can be changed or deleted.

2 - The staging area or INDEX

The area where we prepared a set of files for a new version based its changes

3 - The HEAD

It's the pointer in the current branch, it has the complete repository history

Git Commands by Stages

Working directory

git commandDescription
git initInitialize a normal folder into a Git Repository
git statusshow changes on the working directory and INDEX
git status -sbShow changes as a list
git add .Add all changes to the index
git add {file or folder}Add specific change to INDEX
git rm {file or folder}Remove files from the working directory (for new commit)
git add -A .Add all changes and remove to the INDEX
git clean -d -fDiscard changes (clean the working directory)
git checkout {file}Restore file content (as it is on HEAD)
git stashSave your unfinished work (local changes)
git stash -m "WIP"Save your unfinished work (local changes) giving a name
git stash popBring your saved unfinished work back
git stash listList the stashes that you currently have
git stash pop [stash]Remove a single stashed state and apply it on the current branch
git stash apply [stash]like pop, but do not remove the state from the stash list
git stash -U [stash]store changes adding untracked files
git log --since="1 weeks ago"Show current commits last week
git log [-p] {file}Show changes over a specific file
git log {branch..master}The difference between two branches
git log -S 'text'Show changes matching with the text
git log {directory}show changes over the specific directory
git grep {something}List commits that match with something
git refloglike git log, but show up deleted commits (see git reset)
git blame -L 15,16 {file}Just blame a Little portion of the file
git log -L 15,16:{file}
git push --force-with-leaseIt is a more secure way of push doing --force where it will stop it if will lose some new code
git maintenance startwill add configs that makes git faster by doing some good stuffs in background
git clone --filter=tree:0when working with very large repos, it will make the initial download much faster. Then it will get data on demand when necessary

Staging Area (index)

git commandDescription
git commit -m 'comment...'Create a commit with changes added on the INDEX
git commit -am 'comment...'Add (tracked files) and create a commit
git commit --amend ['comment...']Add changes to the last commit
git restore --staged {file or folder}Remove the file/folder from the staging area - undo the git add
git reset HEAD {file}Undo the add command, keep the changed file on the working directory
git reset HEADUndo all added files, keeping the changes on the working directory
git remote prune originClean local branches by removing remote removed ones
git ls-remote --refs originList remote branches, included PR branches (refs/pull/[PR_NUMBER]/head
git fetch origin pull/[PR_NUMBER]/head:pr/[PR_NUMBER]Get the PR code into the local repository
git checkout pr/[PR_NUMBER]Changes to the PR branch content (fetched in the command above)

HEAD

git commandDescription
git show {sha1}Shows the current commit details
git checkout {sha1}Checkout (navigate) to the code in a specific sha1 (commit)
git checkout HEADCheckout (navigate) to the code in the HEAD
git revert {sha1}Create a new commit undoing an specific commit (sha1)
git revert HEADCreate a new commit undoing the last commit
git reset {sha1}Reset current HEAD to the specified state {sha1}
git reset --merge {sha1}Reset current HEAD to the specified state {sha1}, keeping current changes on the working directory
git reset HEAD~1 --softUndo the last commit, keeping changes on the INDEX
git reset HEAD~1Undo the last commit, keeping changes on the working directory
git reset HEAD~1 --hardUndo the last commit, discarding all changes
git reset HEAD --hardUndo all changes over the INDEX and working directory
git reflogList all {sha} and recover from reset --hard
git bisect startStart binary search the change that introduced a bug
git bisect bad HEADMark the HEAD as bad
git bisect good {sha1}Mark the last commit as good
git bisect {good/bad}Mark an intermediate commit as good or bad
git bisect resetReturn to the commit that was checked out before git bisect start
git remote [add/rm] {repo_alias} {url}Manage the set of repositories whose branches you track
git remote add {repo_alias} -f {url}Add and fetch the remote repository
git remote [-v]List remote repositories
git pushUpdate remote repo using local commits
git rebase {branch}Add on the current branch commits from the specified branch
git rebase -i HEAD~3Make a group of last three commits
git clone {repo url}Clone a repository into a new directory
git clone -b {branch} {repo url}Clone a repository branch
git fetchDownload objects and refs from another repository (.git/FETCH_HEAD)
git pull {alias} [master]Fetch from another repository and merge with the master
git pull [-s strategy] [-X options] [alias] [branch]Fetch from and merge with another repository using custom options
git merge {branch}Merge current branch with another specified branch
git merge {branch} --squashMerge current branch with another specified branch, keeping changes on INDEX
git branch -aList remote and local branches
git branch {new_branch}Create a new local branch
git branch -m {old-name} {new-name}Renaming a local branch
git branch -rd origin/{branch}Delete a branch locally
git push origin --delete bugfixDelete the same branch remotely
git checkout -b {new_branch}Create and change to the new local branch
git checkout {branch}Change to another existing branch
git switch {branch}Change to another existing branch
git switch -c {branch}Create and change to the new created branch
git push -u {repo_alias} {branch}Push local branch as a remote branch
git branch -d {branch}Delete a local branch
git push {repo_alias} --delete {branch}Delete a remote branch
git tag {v1.0}create a new tag
git push origin {v1.0}Update remote repository with the new tag
git format-patch master --stdout > bugfix.patchCreate patches against the master (with commits not in the master)
git apply --stat bugfix.patchList changes over an patch (without apply it)
git am --signoff --ignore-whitespace โ€น bugfix.patchApply patch on the current branch
git merge second_repo/master -s recursive -X oursMerge the current repo

Links and Sources:

ย