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.
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 command | Description |
git init | Initialize a normal folder into a Git Repository |
git status | show changes on the working directory and INDEX |
git status -sb | Show 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 -f | Discard changes (clean the working directory) |
git checkout {file} | Restore file content (as it is on HEAD) |
git stash | Save your unfinished work (local changes) |
git stash -m "WIP" | Save your unfinished work (local changes) giving a name |
git stash pop | Bring your saved unfinished work back |
git stash list | List 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 reflog | like 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-lease | It is a more secure way of push doing --force where it will stop it if will lose some new code |
git maintenance start | will add configs that makes git faster by doing some good stuffs in background |
git clone --filter=tree:0 | when 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 command | Description |
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 HEAD | Undo all added files, keeping the changes on the working directory |
git remote prune origin | Clean local branches by removing remote removed ones |
git ls-remote --refs origin | List 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 command | Description |
git show {sha1} | Shows the current commit details |
git checkout {sha1} | Checkout (navigate) to the code in a specific sha1 (commit) |
git checkout HEAD | Checkout (navigate) to the code in the HEAD |
git revert {sha1} | Create a new commit undoing an specific commit (sha1) |
git revert HEAD | Create 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 --soft | Undo the last commit, keeping changes on the INDEX |
git reset HEAD~1 | Undo the last commit, keeping changes on the working directory |
git reset HEAD~1 --hard | Undo the last commit, discarding all changes |
git reset HEAD --hard | Undo all changes over the INDEX and working directory |
git reflog | List all {sha} and recover from reset --hard |
git bisect start | Start binary search the change that introduced a bug |
git bisect bad HEAD | Mark 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 reset | Return 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 push | Update remote repo using local commits |
git rebase {branch} | Add on the current branch commits from the specified branch |
git rebase -i HEAD~3 | Make 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 fetch | Download 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} --squash | Merge current branch with another specified branch, keeping changes on INDEX |
git branch -a | List 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 bugfix | Delete 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.patch | Create patches against the master (with commits not in the master) |
git apply --stat bugfix.patch | List changes over an patch (without apply it) |
git am --signoff --ignore-whitespace โน bugfix.patch | Apply patch on the current branch |
git merge second_repo/master -s recursive -X ours | Merge the current repo |
Links and Sources:
ย