Cheat Sheet For Git Basic Commands

This article is for a quick reference of all the basic git commands that are used daily by every other coder.

Git Bash will be the best tool for performing all the git commands and that can be downloaded from the git-scm website

To configure the git environment
 

Command Description / Used for
git config --global user.name <userName> To set the user name for the local system (may not be the same as the online repository)
git config --global user.email <[email protected]> To identify the local user and use it for the commits.
git config --global core.editor "code --wait" To set the default editor for all the command line editing. “Code” represents VS Code and other editors include Vim, Emacs, Nano, Sublime, and Atom
git config --global core.autocrlf true
or
git config --global core.autocrlf input
To configure the carriage return of the file ending line. Flag ‘true’ is set for Windows OS and the flag ‘input’ is set for OS X
git config --global color.ui auto For the ease of command line differences, color coding can be automatically set by configuring in the global.

The flag –global is necessary to set for all the repositories, otherwise it will be applied for that particular directory.

To work with the online repositories
 

Command Description / Used for
git clone https://<giturl> To clone the online repository to the local
ls To list all the files available in the directory
git fetch To fetch the latest changes from the online repository while working in the local repository to see the changes (without modifying them)
git merge To merge the latest changes from the online repo
git pull Used to combine the action of git fetch and git merge.


The life cycle of a git repository
 

Command Description / Used for
git init To initialize a working directory as a git repository
git add <test.js>
git add.
git add -all
To add the file from the working directory to the staging area. File can be added individually using the file name or more than one file can be added at a stretch using. or -all commands
git status To display the modified files in the working folder
git diff
git diff --staged
To differentiate the staged files from the un-staged file use git diff. To differentiate the committed files from the staged file use git diff --staged
git commit -m "<commit description >" To commit the staged files which are ready to make the new version of the repository. Commits can be made along with the description of that version like what the change is all about.
git remote -v To list all the connected online repository
git remote add <name> <URL>
git remote rename <oldName> <newName>
git remote remove <name>
To add a new online repository and provide a name for that repo (for future reference)
To rename the online repos reference
To remove the particular remote connection using the name that we provided.
git push <origin> <main>
git push -u <origin> <main>
To publish the committed version online, use git push with the name of the remote connection and branch name.
If the connection requires upstream (tracking) reference then add the flag -u to the command
git push <name> --delete <branchName> To delete a branch available in the online repo


Other miscellaneous git commands
 

Command Description / Used for
git branch To list the available branches for that repo
git branch <branchName>
git branch -m <oldName> <newName>
To create a new branch
To rename the existing branch
git branch -d <branchName> To delete a branch in the local repo
git checkout <newBranchName>
git checkout -b <branchName>
To move on to a new branch
To create a new branch and move directly to that branch.
git log To display the commit history of the current branch
git diff branch2...branch1 To show the difference available between the branches
git rm <filename> To delete the file and stage the changes
git mv <oldPath> <newPath> To move the file from one path to another and stage the changes
git rebase <commit> In the current branch, apply the changes available in the commit ID
git reset --hard <commit> In the current branch, remove all the staged changes and start from the latest changes available in the commit id
git stash To save all the modified changes temporarily in the staged area.
git stash drop To discard all the temporary changes made in the staged area.
git cherry-pick <commit> To move a commit from one branch to another


Similar Articles