Source: KeyCDN Blog

KeyCDN Blog Git Cheat Sheet - a Collection of the Most Useful Commands

<p>Git is the go-to version control tool for most software developers because it allows them to efficiently manage their source code and track file changes while working with a large team. In fact, Git has so many uses that memorizing its various commands can be a daunting task, which is why we&rsquo;ve created this git cheat sheet.</p> <p>This guide includes an introduction to Git, a glossary of terms and lists of commonly used Git commands. Whether you&rsquo;re having trouble getting started with Git, or if you&rsquo;re an experienced programmer who just needs a refresher, you can refer to this cheat sheet for help.</p> <h2 id="what-is-git">What is Git?</h2> <p>If you work in web or software development, then you&rsquo;ve probably used <a href="https://git-scm.com/" target="_blank" rel="nofollow noopener">Git</a> at some point. It remains the most widely used open source distributed version control system over a decade after its initial release. Unlike other version control systems that store a project&rsquo;s full version history in one place, Git gives each developer their own repository containing the entire history of changes. While extremely powerful, Git has some complicated command line syntax that may be confusing at first. Nonetheless, once broken down they&rsquo;re all fairly straightforward and easy to understand.</p> <h2 id="git-glossary">Git Glossary</h2> <p>Before you get started with Git, you need to understand some important terms:</p> <h3 id="branch">Branch</h3> <p>Branches represent specific versions of a repository that &ldquo;branch out&rdquo; from your main project. Branches allow you to keep track of experimental changes you make to repositories and revert to older versions.</p> <h3 id="commit">Commit</h3> <p>A commit represents a specific point in your project&rsquo;s history. Use the commit command in conjunction with the git add command to let git know which changes you wish to save to the local repository. Note that commits are not automatically sent to the remote server.</p> <h3 id="checkout">Checkout</h3> <p>Use the git checkout command to switch between branches. Just enter git checkout followed by the name of the branch you wish to move to, or enter git checkout master to return to the master branch. Mind your commits as you switch between branches.</p> <h3 id="fetch">Fetch</h3> <p>The git fetch command copies and downloads all of a branch&rsquo;s files to your device. Use it to save the latest changes to your repositories. It&rsquo;s possible to fetch multiple branches simultaneously.</p> <h3 id="fork">Fork</h3> <p>A fork is a copy of a repository. Take advantage of &ldquo;forking&rdquo; to experiment with changes without affecting your main project.</p> <h3 id="head">Head</h3> <p>The commit at the tip of a branch is called the head. It represents the most current commit of the repository you&rsquo;re currently working in.</p> <h3 id="index">Index</h3> <p>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.</p> <h3 id="master">Master</h3> <p>The master is the primary branch of all your repositories. It should include the most recent changes and commits.</p> <h3 id="merge">Merge</h3> <p>Use the git merge command in conjunction with pull requests to add changes from one branch to another.</p> <h3 id="origin">Origin</h3> <p>The origin refers to the default version of a repository. Origin 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.</p> <h3 id="pull">Pull</h3> <p>Pull requests represent suggestions for changes to the master branch. If you&rsquo;re working with a team, you can create pull requests to tell the repository maintainer to review the changes and merge them upstream. The git pull command is used to add changes to the master branch.</p> <h3 id="push">Push</h3> <p>The git push command is used to update remote branches with the latest changes you&rsquo;ve committed.</p> <h3 id="rebase">Rebase</h3> <p>The git rebase command lets you split, move or get rid of commits. It can also be used to combine two divergent branches.</p> <h3 id="remote">Remote</h3> <p>A remote is a clone of a branch. Remotes communicate upstream with their origin branch and other remotes within the repository.</p> <h3 id="repository">Repository</h3> <p>Git repositories hold all of your project&rsquo;s files including branches, tags and commits.</p> <h3 id="stash">Stash</h3> <p>The git stash command removes changes from your index and &ldquo;stashes&rdquo; them away for later. It&rsquo;s useful if you wish to pause what you&rsquo;re doing and work on something else for a while. You can&rsquo;t stash more than one set of changes at a time.</p> <h3 id="tags">Tags</h3> <p>Tags provide a way to keep track of important commits. Lightweight tags simply serve as pointers while annotated tags get stored as full objects.</p> <h3 id="upstream">Upstream</h3> <p>In the context of Git, upstream refers to where you push your changes, which is typically the master branch.</p> <p>See the <a href="https://git-scm.com/docs" target="_blank" rel="nofollow noopener">Git docs reference</a> guide for more in depth explanations of Git related terminology.</p> <h2 id="commands-for-configuring-git">Commands for Configuring Git</h2> <p>Set the username:</p> <pre><code class="language-none">git config -global user.name </code></pre> <p>Set the user email:</p> <pre><code class="language-none">git config -global user.email </code></pre> <p>Create a Git command shortcut:</p> <pre><code class="language-none">git config -global alias. </code></pre> <p>Set the preferred text editor:</p> <pre><code class="language-none">git config -system core.editor </code></pre> <p>Open and edit the global configuration file in the text editor:</p> <pre><code class="language-none">git config -global -edit </code></pre> <p>Enable command line highlighting:</p> <pre><code class="language-none">git config -global color.ui auto </code></pre> <h2 id="commands-for-setting-up-git-repositories">Commands for Setting Up Git Repositories</h2> <p>Create an empty repository in the project folder:</p> <pre><code class="language-none">git init </code></pre> <p>Clone a repository from GitHub and add it to the project folder:</p> <pre><code class="language-none">git clone (repo URL) </code></pre> <p>Clone a repository to a specific folder:</p> <pre><code class="language-none">git clone (repo URL) (folder) </code></pre> <p>Display a list of remote repositories with URLs:</p> <pre><code class="language-none">git remote -v </code></pre> <p>Remove a remote repository:</p> <pre><code class="language-none">git remote rm (remote repo name) </code></pre> <p>Retrieve the most recent changes from origin but don&rsquo;t merge:</p> <pre><code class="language-none">git fetch </code></pre> <p>Retrieve the most recent changes from origin and merge:</p> <pre><code class="language-none">git pull </code></pre> <h2 id="commands-for-managing-file-changes">Commands for Managing File Changes</h2> <p>Add file changes to staging:</p> <pre><code class="language-none">git add (file name) </code></pre> <p>Add all directory changes to staging:</p> <pre><code class="language-none">git add . </code></pre> <p>Add new and modified files to staging:</p> <pre><code class="language-none">git add -A </code></pre> <p>Remove a file and stop tracking it:</p> <pre><code class="language-none">git rm (file_name) </code></pre> <p>Untrack the current file:</p> <pre><code class="language-none">git rm -cached (file_name) </code></pre> <p>Recover a deleted file and prepare it for commit:</p> <pre><code class="language-none">git checkout &lt;deleted file name&gt; </code></pre> <p>Display the status of modified files:</p> <pre><code class="language-none">git status </code></pre> <p>Display a list of ignored files:</p> <pre><code class="language-none">git ls-files -other -ignored -exclude-standard </code></pre> <p>Display all unstaged changes in the index and the current directory:</p> <pre><code class="language-none">git diff </code></pre> <p>Display differences between files in staging and the most recent versions:</p> <pre><code class="language-none">git diff -staged </code></pre> <p>Display changes in a file compared to the most recent commit:</p> <pre><code class="language-none">git diff (file_name) </code></pre> <h2 id="commands-for-declaring-git-commits">Commands for Declaring Git Commits</h2> <p>Commit changes along with a custom message:</p> <pre><code class="language-none">git commit -m &quot;(message)&quot; </code></pre> <p>Commit and add all changes to staging:</p> <pre><code class="language-none">git commit -am &quot;(message)&quot; </code></pre> <p>Switch to a commit in the current branch:</p> <pre><code class="language-none">git checkout &lt;commit&gt; </code></pre> <p>Show metadata and content changes of a commit:</p> <pre><code class="language-none">git show &lt;commit&gt; </code></pre> <p>Discard all changes to a commit:</p> <pre><code class="language-none">git reset -hard &lt;commit&gt; </code></pre> <p>Discard all local changes in the directory:</p> <pre><code class="language-none">git reset -hard Head </code></pre> <p>Show the history of changes:</p> <pre><code class="language-none">git log </code></pre> <p>Stash all modified files:</p> <pre><code class="language-none">git stash </code></pre> <p>Retrieve stashed files:</p> <pre><code class="language-none">git stash pop </code></pre> <p>Empty stash:</p> <pre><code class="language-none">git stash drop </code></pre> <p>Define a tag:</p> <pre><code class="language-none">git tag (tag_name) </code></pre> <p>Push changes to origin:</p> <pre><code class="language-none">git push </code></

Read full article »
Est. Annual Revenue
$5.0-25M
Est. Employees
25-100
Jonas Krummenacher's photo - Co-Founder of KeyCDN

Co-Founder

Jonas Krummenacher

CEO Approval Rating

63/100

Read more