26 Must-Know Git Commands in 2021
Published: 2021-10-08 07:27:50

<h1>26 Must-Know Git Commands in 2021</h1><img src="https://uploads-ssl.webflow.com/60d5a97381523a05b73c87c6/6128bfe8ea2f0553a99500b7_24.png"/><p><br></p><p>Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. It's a system that tracks changes to our project files over time. It enables us to record project changes and go back to a specific version of the tracked files, at any given point in time.&nbsp; This change history lives on your local machine and lets you revert to a previous version of your project with ease in case something goes wrong. Git makes collaboration easy. Knowing how to use Git is one of the most important skills for any developer nowadays - and it's definitely a great addition to your resume!</p><p>Here we will look at some of the git commands to get you started with git.</p><ol><li>Check your Git configuration</li></ol><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The command below<strong> </strong>returns a list of information about your git configuration including user name and email.</p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git config –l</strong></p><ol start="2"><li>Setup your Git username</li></ol><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git config --global user.name "your-name"</strong></p><ol start="3"><li>Setup your Git user email</li></ol><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git config --global user.email "your-email"</strong></p><ol start="4"><li>Cache your login credentials in Git</li></ol><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;You can store login credentials in the cache so you don't have to type them in each time.</p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git config --global credential.helper cache</strong></p><ol start="5"><li>Initialize a Git repo</li></ol><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The first step to creating your git repository is initializing it. You can initialize a git repo for a new or existing project using the following command:</p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git init</strong></p><ol start="6"><li>Add a file to the staging area</li></ol><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The next step is to add the files in the project to the staging area. You can add a file using the following command:</p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; git add &lt;filename&gt;</strong></p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;To add all the files in the current directory, use the following command:</p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git add .</strong></p><ol start="7"><li>Check a repository's status</li></ol><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;This command will show the status of the current repository including staged, unstaged, and untracked files.</p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git status</strong></p><ol start="8"><li>Commit changes in the editor</li></ol><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The common way to commit changes made to your repo is with the –m option which lets you specify a shot summary for your commit message.</p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git commit -m "your commit message here"</strong></p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;You can add and commit files in one step using the following command:</p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git commit -a –m "your commit message here"</strong></p><ol start="9"><li>See your commit history in Git</li></ol><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;To see your commit history for the current repo, use the following command:</p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git log</strong></p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;To see your commit history including all the files and their changes, type:</p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git log –p</strong></p><ol start="10"><li>Remove tracked files from the current working tree in Git</li></ol><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git rm filename</strong></p><ol start="11"><li>Rename files in Git</li></ol><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git mv oldfile newfile</strong></p><ol start="12"><li>Ignore files in Git</li></ol><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Create a .gitignore file and commit it.</p><ol start="13"><li>Rolling back commits</li></ol><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Use the below command to roll back the last commit:</p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git revert HEAD</strong></p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;To revert an old commit in git:</p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git revert commit_id_here</strong></p><ol start="14"><li>Create a new branch in git</li></ol><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Branch allows teams to work on the same code base in parallel. If you want to add some functionality to a branch without changing the actual code, you can create a branch and if your team likes the changes, you can merge the two branches together. To create a new branch, use the command below:</p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git branch branch_name</strong></p><ol start="15"><li>Switch to a newly created branch</li></ol><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git checkout branch_name</strong></p><ol start="16"><li>Delete a branch</li></ol><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git branch –d branch_name</strong></p><ol start="17"><li>Merge two branches in git</li></ol><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git merge branch_name</strong></p><ol start="18"><li>Show the commit log as a graph</li></ol><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git log –graph –online</strong></p><ol start="19"><li>Add a remote repository</li></ol><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git add remote https://repo_here</strong></p><ol start="20"><li>See all remote repositories</li></ol><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git remote –v</strong></p><ol start="21"><li>Push changes to a remote repo</li></ol><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;When all your work is ready to be saved on a remote repository, you can push all changes using the command below:</p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git push</strong></p><ol start="22"><li>Pull changes from a remote repo</li></ol><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If other team members are working on your repository, you can retrieve the latest changes made to the remote repository with the command below:</p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git pull</strong></p><ol start="23"><li>Fetch remote repo changes</li></ol><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;This command will download the changes from a remote repo but will not perform a merge on your local branch (as git pull does that instead).</p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git fetch</strong></p><ol start="24"><li>Merge a remote repo with your local repo</li></ol><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If the remote repository has changes you want to merge with your local, then use the following command</p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git merge origin/main</strong></p><ol start="25"><li>Push a new branch to a remote repo</li></ol><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git push -u origin branch_name</strong></p><ol start="26"><li>Clone a git repository</li></ol><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The git clone command is used to download the source code from a remote repository. When you clone a repo, the code is automatically downloaded to your local machine. To clone a git repo, use the following command:</p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;git clone &lt;https://url-of-the-repository&gt;</strong></p><p>‍</p><p><br></p>