Git Commands Cheet Sheet

This guide includes an introduction to Git, a glossary of terms and lists of commonly used Git commands.

What is Git? 🤔

Git gives each developer their own repository containing the entire history of changes. More formally Git is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals.

Before you get started with Git, you need to understand some important terms:😀

  • Commit:- It represents a specific point in your project's history.
  • Checkout:- Checkout command to switch between branches.
  • Fetch:- Fetch command copies and downloads all of a branch's files to your device. 
  • Fork:- A fork is a copy of a repository. Take a copy of the project for doing some changes without affecting your main project.
  • Head:- It represents the most current commit of the repository you're currently working in. The commit at the top of a branch is called the head.
  • Index:- Whenever you add, delete or alter a file, it remains in the index until you are ready to commit the changes. Think of it as the staging area for Git. Use the git status command to see the contents of your index. Changes highlighted in green are ready to be committed while those in red still need to be added to staging.

  • Master:- The master is the primary branch of all your repositories.
  • Merge:- Use the git merge command to add changes from one branch to another.
  • Origin:- Origin refers to the default version of a repository. Also serves as a system alias for communicating with the master branch. Use the command git push origin master to push local changes to the master branch.
  • Pull:- The git pull command is used to add changes from someone's branch to the master branch.
  • Push:- Git push command is used to update remote branches with the latest changes you've committed.
  • Rebase:- Rebasing is the process of moving or combining a sequence of commits to a new base commit.
  • Remote:- The git remote command lets you create, view, and delete connections to other repositories. Remote connections are more like bookmarks rather than direct links into other repositories.
  • Repository:- Git repositories hold all of your project's files including branches, tags and commits.
  • Stash:- Git stash temporarily stashes changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on.
  • Tags:-Tagging is generally used to capture a point in history that is used for a marked version release (i.e. v1.0.1). A tag is like a branch that doesn’t change. Unlike branches, tags, after being created, have no further history of commits.
  • Upstream:- Upstream refers to where you push your changes, which is typically the master branch.
  • gitignore:- Specifies untracked files to ignore.

Configuring Git 

  • Set the username:- git config –global user.name [name]
  • Set the user email:-  git config –global user.email [email]
  • Create a Git command shortcut:- git config –global alias. [alias name] [git command] 
  • Set the preferred text editor:- git config –-system core.editor [editor name]
  • Open and edit the global configuration file in the text editor:- git config –global –edit
  • Enable command line highlighting:- git config –global color.ui auto

Commands for setting up Git repositories

  • Create an empty repository in the project folder:- git init
  • Clone a repository from GitHub and add it to the project folder:- git clone [repo URL]
  • Clone a repository to a specific folder:- git clone [repo URL] [folder]
  • Display a list of remote repositories with URLs:- git remote -v
  • Remove a remote repository:- git remote rm [remote repo name]
  • Retrieve the most recent changes from origin but don't merge:- git fetch
  • Retrieve the most recent changes from the origin and merge:- git pull

Commands for managing file changes

  • Add file changes to staging:- git add [filename]
  • Add all directory changes to staging:- git add .
  • Add new and modified files to staging:- 
  • Remove a file and stop tracking it:- git rm [file_name]
  • Untrack the current file: git rm –cached [file_name]
  • Recover a deleted file and prepare it for commit: git checkout [deleted file name]
  • Display the status of modified files: git status
  • Display a list of ignored files: git ls-files –other –ignored –exclude-standard
  • Display all unstaged changes in the index and the current directory: git diff SOURCE_BRANCH origin/TARGET_BRANCH  PATH
  • Display differences between files in staging and the most recent versions: git diff –staged
  • Display changes in a file compared to the most recent commit: git diff [file_name]

Commands for declaring Git commits

  • Commit changes along with a custom message: git commit -m "[message]"
  • Commit and add all changes to staging: git commit -am "[message]"
  • Switch to a commit in the current branch: git checkout [commit]
  • Show metadata and content changes of a commit: git show [commit]
  • Discard all changes to a commit: git reset –hard [commit]
  • Discard all local changes in the directory: git reset –hard Head
  • Show the history of changes: git log
  • Stash all modified files: git stash
  • Retrieve stashed files: git stash pop
  • Empty stash: git stash drop
  • Define a tag: git tag [tag_name]
  • Push changes to origin: git push

Commands for Git branching

  • Push all local branches to a designated remote repository: git push –all
  • Preview changes before merging branches: git diff [sourcebranch][targetbranch]
  • Fetch a branch from the repository: git fetch remote [branchname]
  • Merge a different branch with your active branch: git merge [branchname]
  • Delete a branch: git branch -d [branchname]
  • Switch to a branch: git checkout [branchname]
  • Make a new branch and switch to it: git checkout -b [branchname]
  • Display a list of all branches: git branch

Reference

KeyCDN

Note:- In this post, there is only one thing left which is explaining by example. Soon I will also add that thing too.

Comments

Popular posts from this blog

MySQL Multi Source Master Slave Replication using GTID

Access and modify all the resources of our Wiki.js using WikiJS API

How to pass parameters in webhook?