Common Git Commands
Here are some commonly used Git commands and their explanations, which can serve as a quick reference guide. Make sure to read our Git Best Practices page to understand how we use these commands at Planet Argon.
Creating Repo
Section titled “Creating Repo”
git init: This command is used to initialize a new Git repository.git clone <url>: This command copies an existing Git repository from the specified URL.git clone <url> <local_repo_name>: This command clones an existing Git repository and names it locally as specified.Staging / Committing
Section titled “Staging / Committing”
git add -p(add patch): This command allows you to interactively stage parts of changes made to your tracked files.git add -i(interactive command line tool): This command is an interactive shell for staging changes for commit.git rm <filename>: This command deletes a file and stages the removal for commit.git mv <old_filename> <new_filename>: This command renames or moves files, and automatically stages the changes for a commit.git commit: This command records changes to the repository.git commit -m "descriptive message with ticket number": This command records changes to the repository with a specific commit message.git commit --amend: This command alters the most recent commit.git checkout <filename>: This command discards all changes made to the named file since the last commit.git reset <filename>: This command unstages staged changes of the named file but keeps the changes in the file.Branching
Section titled “Branching”
git checkout <branch_name>: This command switches to the specified branch.git checkout -b <branch_name>: This command creates a new branch and switches to it immediately.git branch -d <branch_name>: This command deletes a merged branch.git branch -D <branch_name>: This command forcefully deletes an unmerged branch. Use this with caution!git branch: This command lists all local branches.git branch -a: This command lists all branches, including local and remote.git branch -v: This command shows the last commit on each branch.git branch -vv: This command shows the last commit and tracks the branch for each local branch.Remotes
Section titled “Remotes”
git remote -v: This command lists all remote repositories.git remote add <remote_name> <remote_url>: This command adds a remote repository with the specified name and URL.git pull: This command fetches from and integrates with another repository or a local branch.git pull <remote_name> <branch_name>: This command fetches from a specific branch of a specific remote repository and merges it.git pull --rebase: This command fetches the branch and then rebases it.git push: This command updates remote references along with associated objects.git push <remote_name> <branch_name>: This command pushes the branch to the specified remote repository.Merging / Rebasing
Section titled “Merging / Rebasing”
git merge <branch_from> <branch_to>: This command merges the specified branches.git merge --no-ff <branch_from> <branch_to>: This command merges only if the branches can be fast-forwarded.git rebase <branch_from> <branch_to>: This command applies changes from one branch onto another.git rebase --abort: This command stops the current rebase process.git rebase --continue: This command continues the rebase process after resolving conflicts.git rebase -i(interactive mode): This command provides an interface to alter commits during the rebase process.Tagging
Section titled “Tagging”
git tag <tag_value>: This command creates a tag for easy reference to a certain point in the history.Stashing
Section titled “Stashing”
git stash: This command temporarily saves changes that you don’t want to commit immediately.git stash apply: This command re-applies previously stashed changes.git stash pop: This command applies stashed changes and then drops them from your stack.git stash drop: This command discards the most recently stashed changeset.git stash list: This command lists all stashed changesets.git stash show: This command shows the summary of a stash.git stash show -p: This command shows the full diff of a stash.Visibility
Section titled “Visibility”
git status: This command displays the state of the working directory and the staging area.git diff: This command shows differences between tracked files.git diff <SHA1> <SHA2>: This command shows differences between two snapshots of the tracked files.git log: This command shows commit history in reverse chronological order.git log -p <filename>: This command shows the history of the specified file.git log --follow <filename>: This command shows the history for the specified file including any path changes.git blame <filename>: This command shows what revision and author last modified each line of a file.Useful Miscellany
Section titled “Useful Miscellany”
HEAD: This represents the current commit.tilde:
HEAD~orHEAD~1: This points to the immediate parent of the current snapshot.HEAD~0: This points to the current snapshot.HEAD~n: This points to the nth parent of the current snapshot.caret:
HEAD^orHEAD^1: This points to the first parent of the current snapshot. It’s functionally equivalent toHEAD~.HEAD^0: This points to the current snapshot.HEAD^2: This points to the second parent in the case of a merge, if one exists.