Git Commands Cheat Sheet

Ebook_Cover

Buy PDF

Here are some of the most common and useful Git commands

Git Configuration

CommandDescription
$ git configπŸ› οΈ Get and set configuration variables for Git.
$ git config --global user.name "User name"✏️ Set the name.
$ git config --global user.email "your-email@example.com"βœ‰οΈ Set the email.
$ git config --global core.editor "your-editor"πŸ“ Set the default editor.
$ git config --global color.ui auto🌈 Turns on color output for Git commands.
$ git config --listℹ️ See all your settings.

Create Project

CommandDescription
$ git init🏁 Create a local repository.
$ git cloneπŸ”„ Clone a remote repository.

Local Changes

CommandDescription
$ git add file-pathβž• Adds a file to the staging area.
$ git add .βž• Add all files to the staging area.
$ git commit -m " Commit Message"πŸ’Ύ Commit file permanently in the version history.

Track Changes

CommandDescription
$ git diffπŸ”„ Shows the differences between your folder and the stage.
$ git diff --stagedπŸ”„ Shows the differences between the stage and the last commit.
$ git diff HEADπŸ”„ Track the changes after committing a file.
$ git diff <branch-2>πŸ”„ Track the changes between two commits.
$ git statusℹ️ Display the state of the working directory and the staging area.
$ git showℹ️ Show the newest commit on a branch.
$ git diff -- file-pathπŸ”„ Show changes for a particular file.
$ git diff branch-1 branch-2 --name-onlyπŸ”„ Show difference between two branches (file names only).
$ git diff branch-1 branch-2 -- file-pathπŸ”„ Show differences between two branches for a specific file.

Commit History

CommandDescription
$ git logπŸ“œ Display the most recent commits and the status of the head.
$ git log -onelineπŸ“œ Display the output as one commit per line.
$ git log -statπŸ“œ Displays the files that have been modified.
$ git log -pπŸ“œ Display the modified files with location.

Ignoring files

CommandDescription
$ touch .gitignoreπŸ“ Create a file named .gitignore to list ignored files.
$ git update-index --skip-worktree <file_name>πŸ“ Mark a file as β€œskip-worktree” to ignore changes in it.
$ git update-index --no-skip-worktree <file_name>πŸ“ Unmark a file as β€œskip-worktree” to stop ignoring changes in it.

A collection of useful .gitignore templates for different languages or frameworks: gitignore

Branching

Git branch

CommandDescription
$ git branch🌿 Show a list of all branches in your local repository.
$ git branch -a🌿 Show all branches in both local and remote repositories.
$ git branch -r🌿 Show only remote branches.
$ git branch name🌿 Creates a new branch called name based on the current commit.
$ git branch name hash🌿 Create a new branch based on a specific commit identified by its hash.

Git checkout

CommandDescription
$ git checkout branch-nameπŸ”€ Switch between branches in a repository.
$ git checkout -b branch-nameπŸ”€ Create a new branch and switch to it.

Git stash

CommandDescription
$ git stashπŸ“¦ Stash current work.
$ git stash save "your message"πŸ“¦ Saving stashes with a message.
$ git stash listπŸ“¦ Check the stored stashes.
$ git stash applyπŸ“¦ Re-apply the changes that you just stashed.
$ git stash showπŸ“¦ Track the stashes and their changes.
$ git stash popπŸ“¦ Re-apply the previous commits.
$ git stash dropπŸ“¦ Delete the most recent stash from the queue.
$ git stash clearπŸ“¦ Delete all the available stashes at once.
$ git stash branchπŸ“¦ Stash work on a separate branch.

Merging

Git merge

CommandDescription
$ git merge name🀝 Merges branch called name into the current branch, creating a new commit that incorporates both changes.
$ git merge --abort🀝 Aborts the merge process and restores the original state of your project, if there are any conflicts or errors during the merge.

Git rebase

CommandDescription
$ git rebaseπŸ”€ Apply a sequence of commits from one branch to another.
$ git rebase -continueπŸ”€ Continue the rebasing process after resolving conflicts manually.
$ git rebase --skipπŸ”€ Skip a commit when rebasing.

Remote

CommandDescription
$ git remote🌐 Show a list of all remote repositories associated with your local repository.
$ git remote -v🌐 Check the configuration of the remote server.
$ git remote show name🌐 Show information about a specific remote repository called name.
$ git remote add origin repo-url🌐 Add a remote for the repository.
$ git remote rm🌐 Remove a remote connection from the repository.
$ git remote rename🌐 Rename a remote server.
$ git remote show🌐 Show additional information about a particular remote.
$ git remote set-url name url🌐 Change the URL of a remote repository called name to url.

Pushing Updates

CommandDescription
$ git push origin masterπŸ“€ Push data to the remote repository.
$ git push --allπŸ“€ Push all branches to the default remote repository.
$ git push --all originπŸ“€ Specify the remote repository explicitly.
$ git push -fπŸ“€ Force push data to the remote repository.

Pulling Updates

Git pull

CommandDescription
$ git pullπŸ“₯ Pull changes from the default remote repository and branch.
$ git pull origin masterπŸ“₯ Specify the remote repository and branch explicitly.

Git fetch

CommandDescription
$ git fetch <repository URL>πŸ“₯ Fetch the remote repository.
$ git fetchπŸ“₯ Fetch a specific branch.
$ git fetch --allπŸ“₯ Fetch all branches simultaneously.
$ git fetch originπŸ“₯ Synchronize the local repository.

Undo Changes

Git revert

CommandDescription
$ git revert HEADβͺ Undo the latest commit.
$ git revert hashβͺ Create a new commit that undoes changes made by a specific commit identified by its hash.
$ git revert branchβͺ Undo all commits on a branch.

Git reset

CommandDescription
$ git reset --soft hashβͺ Reset your current branch to a specific commit identified by its hash, keeping your working directory and staging area unchanged.
$ git reset --mixed hashβͺ Reset your current branch to a specific commit identified by its hash, resetting your staging area to match it, but keeping your working directory unchanged.
$ git reset --hard hashβͺ Reset your current branch to a specific commit identified by its hash, resetting your working directory and staging area to match it, discarding any changes made since then.

Removing Files

CommandDescription
$ git rm fileπŸ—‘οΈ Delete a file from both your working directory and staging area, staging the deletion for the next commit.
$ git rm --cached fileπŸ—‘οΈ Delete a file only from the staging area, but not from the working directory.

Reference