Git Cheat Sheet for Beginners – Cloudways

In the previous installments of this series, I’ve covered important topics like Git tutorials, branch administration and conflict resolution, and the best Git GUI clients for different platforms. In this Git cheat sheet, I’ll cover the most commonly used Git commands. This list will be divided into categories and has a brief description of each command. I’ve added a section on Git terminology so you can understand how Git works and how commands are executed on various entities.

Git is a huge platform with a number of commands covering all aspects of Git functions. Let’s start with

Git terminology first: Git

terminologies

# Description of the Git 1 command Bare repository Repository without a working directory. 2 Branch A is an active area of development in Git. The most recent confirmation shows the tip of the branch. 3 Blame Describes the last modification of each line of the file. Shows review, author and time. 4 Checkout This refers to the process where any given confirmation from the repository is selected and the status of the associated file and directory tree is recreated in the working directory. 5 Commit This is a single point in the Git history that contains information about a change set. 6 Difference A Difference is the difference in changes between two commits or saved changes. 7 Separate head The state in which a specific commit is checked out instead of a branch. 8 Fetch Fetching means getting the latest changes in the branch and local/remote repositories. 9 Branch When you branch the repository, you can add commits and create pull requests. 10 Hash A unique SHA1 code for each Commit 11 Head A referenced to the Commit at the tip of a branch 12 Index A collection of files with status information. 13 Merge To highlight content from another branch in the current branch. 14 Master The default development branch in Git 15 Origin The default upstream repository 16 Check out request Suggests changes to master branch 17 Push Pushes new changes after committing them 18 Repository A collection of commits, branches, and tags to identify Commits. 19 Worktree The Tree of Actual Retired Files Git Settings # Description of the Git 1 command git config -global user.name Set the username to be used for all actions 2

git

config -global user.email Set the email to be used for all actions. 3 git config -alias global. Create a shortcut for the Git command. 4 git config -system core.editor Set the text editor for all command actions. 5 git config -global -edit Open the global configuration file in the text editor for manual editing. 6 git config -global color.ui auto Enable useful coloring of command-line outputs. Set up a Git repository # Description of the Git 1 command git init Initialize

an empty Git repository

in the current project. 2 git clone (repository URL) Clone the GitHub repository to the project folder. 3 git clone (Repository URL) (Folder) Clone the repository to a specific folder. 4 git remote add origin https://github.com/username/(repo_name).git Create a remote repository that points to your existing GitHub repository. 5 git remote Displays the name of remote repositories. 6 git remote -v Displays the name and URL of remote repositories. 7 git remote rm (remote repository name) Deletes the remote repository. 8 git remote set-url origin (git URL) Changes the repository URL. 9 git fetch Get the latest changes from the source but don’t merge. 10 git pull Get the latest changes from the source and merge them.

Local File Changes

# Git 1 Command Description git add (file name) Add the current changes to the file to staging. 2 git add . Add all directory changes to staging (without deleting files). 3 git add -A Add all new, modified, and deleted files to the staging. 4 git rm (file_name) Deletes the file and untracks it (stops crawling it). 5 git rm -cached (file_name) Untraces the current file. 6 git mv (file_name) (new_file_name) Renames the file and prepares it for Commit. 7 git checkout <deleted file name> Recovers the deleted file and prepares it for the Commit git state 8 Displays the status of modified files. 9 git ls-files -other -ignored -exclude-standard Displays the list of all ignored files. 10 git diff Displays unconfigured changes to the index and working directory. 11 git diff -staged Displays the file differences between staging and the latest version of the file. 12 git diff (file_name) Shows changes in a single file compared to the last Commit.

Declare Commits

# Git Command Description 1 git commit -m “(message)” Commit changes with a custom message. 2 git commit -am “(message)” Adds all changes to the trial and commits them with a custom message. 3 git checkout Switch to the confirmation provided. 4 git show Generates the metadata and content changes for the specified commit. 5 git reset -hard Discard all history and changes to a given commit. 6 git reset -hard Head Discards all local changes to the working directory. 7 git log Displays change history. 8 git log -p Displays the full display of each commit. 9 git log -oneline Displays the list of commits with a simple message. 10 git log -follow (file_name) List the history of the current file. 11 git blame (file_name) Displays all changes along with the user’s name. 12 git stash Temporarily saves all tracked modified files. 13 git stash pop Restores the most recently saved files. 14 List of git caches List all modified stash sets. 15 git stash apply Apply the most recent saved content. 16 Git stash drop Discard most recently stored files 17 Apply git stash (hiding place identifier) Reapply specific stash content by ID. 18 Git stash removal (stash_id) Delete specific stash content by ID. 19 git push Send changes to the source. 16 git push origin (branch_name) Insert branch to source. 17 Git push -f origin (branch_name) Force pushes changes to the source. 18 git tag (tag_name) Define a tag for a version. 19 git push Push changes at source.

Branch

# Git Command Description 1 git branch Displays the list of all branches. 2 git branch Creates a new branch. 3 git branch -m Renames the branch. 4 git branch -a List of all branches, local and remote. 5 git checkout -b Create a branch and switch to it. 6 git checkout Switch to the provided branch. 7 git checkout -b origin/ Get a remote branch from origin to the local directory. 8 git branch -d Delete the specified branch. 9 git merge Merge the current branch into the master (first checkout to the master) 10 git rebase Takes all changes from the branch and re-expresses them in others. 11 git rebase Overshoot the current branch in the base. Base can be a confirmation ID or a branch name. 12 git fetch remote Gets the specified branch of the repository. 13 git diff .. Shows the differences between two branches. 14 git pull -rebase Gets the remote copy of the current branch and overshoots it in the local copy. 15 git push -all Push all local branches to the specified remote repository.

You can also download the above cheat sheet in PDF by clicking the download button.

cta

Conclusion

Git commands

cover the full spectrum of Git functions. It’s important to note that it’s impossible to memorize the full range of Git commands. An easier way is to use this Git cheat sheet when working on complicated projects. If you think I’ve overlooked an important command or would like to add to the discussion, leave a comment below.