{"id":757,"date":"2017-03-06T00:00:00","date_gmt":"2017-03-06T00:00:00","guid":{"rendered":"http:\/\/blog.hostinger.io\/hostinger-tutorials\/uncategorized\/basic-git-commands\/"},"modified":"2025-02-24T12:48:06","modified_gmt":"2025-02-24T12:48:06","slug":"basic-git-commands","status":"publish","type":"post","link":"\/uk\/tutorials\/basic-git-commands","title":{"rendered":"Most Used Git Commands From Basic to Advanced to Simplify Your Workflow + Free Git Cheat Sheet"},"content":{"rendered":"<p>Using Git commands when developing software or applications helps you simplify repository management and workflow. Whether you&rsquo;re an individual developer or a team member, learning Git simplifies version control and enhances effectiveness.<\/p><p>This Git commands tutorial offers a list of Git commands along with their purposes and usage examples. It covers the basics and introduces some advanced techniques.<\/p><p>\n\n\n\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-understanding-the-git-workflow\">Understanding the Git Workflow<\/h2><p>A Git project consists of three major sections: <strong>the working directory, the staging area, <\/strong>and <strong>the git directory.<\/strong><\/p><p>The working directory is where you add, delete, and edit files. Then, the changes are indexed in the staging area. After you commit your modifications, a snapshot of the changes will be saved in the directory.<\/p><p>Git can be downloaded from its <a href=\"https:\/\/git-scm.com\/downloads\" target=\"_blank\" rel=\"noopener\">official site<\/a>. It is available for Linux or Unix, Windows, and macOS.<\/p><p>If you&rsquo;re using a <a href=\"\/uk\/vps-hosting\">Hostinger VPS hosting plan<\/a>, you can <a href=\"\/uk\/tutorials\/how-to-install-git-on-ubuntu\">install Git directly<\/a> to your server by typing one of the commands below depending on your installed OS:<\/p><p>For Debian-based distributions, including Ubuntu:<\/p><pre class=\"wp-block-preformatted\">sudo apt install git<\/pre><p>For Fedora-based distributions, such as CentOS:<\/p><pre class=\"wp-block-preformatted\">sudo yum install git<\/pre><p>or<\/p><pre class=\"wp-block-preformatted\">sudo dnf install git<\/pre><?xml encoding=\"utf-8\" ?><figure class=\"wp-block-image size-large\"><a href=\"\/uk\/vps-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner-1024x300.png\" alt=\"\" class=\"wp-image-77934\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner-300x88.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner-150x44.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner-768x225.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><h2 class=\"wp-block-heading\" id=\"h-most-commonly-used-git-commands\">Most Commonly Used Git Commands<\/h2><p>For some users, Git may have a steep learning curve. Fortunately, this section will guide you through the most frequently used Git commands in the command line interface (CLI) tool.<\/p><p>If you prefer a graphical user interface (GUI) for writing Git commands, consider using one of the <a href=\"\/uk\/tutorials\/best-git-gui-clients\/\">best Git GUI clients<\/a>.<\/p><p>Let&rsquo;s explore some essential Git commands and their functionalities.<\/p><h3 class=\"wp-block-heading\" id=\"h-basic-git-commands\">Basic Git Commands<\/h3><p><strong>git init<\/strong><\/p><p>This command initiates a new Git repository within a directory. Here&rsquo;s the basic <strong>git init<\/strong> usage:<\/p><pre class=\"wp-block-preformatted\">git init<\/pre><p>To create a new repository while specifying the project&rsquo;s name, use the following command:<\/p><pre class=\"wp-block-preformatted\">git init [project name]<\/pre><p><strong>git add<\/strong><\/p><p>This command is used to stage file changes, preparing them for the next commit:<\/p><pre class=\"wp-block-preformatted\">git add file1.txt<\/pre><p><strong>git commit<\/strong><\/p><p>Use this command to create a commit message for the changes, making them part of your project&rsquo;s history:<\/p><pre class=\"wp-block-preformatted\">git commit -m \"Add new feature\"<\/pre><p><strong>git status<\/strong><\/p><p>This command displays valuable insights into your files&rsquo; modifications and staging status.<\/p><pre class=\"wp-block-preformatted\">git status<\/pre><p><strong>git log<\/strong><\/p><p>The basic <strong>git log <\/strong>usage lets you view a chronological list of commit history:<\/p><pre class=\"wp-block-preformatted\">git log<\/pre><p><strong>git diff<\/strong><\/p><p>This command lets you compare changes between your working directory and the most recent commit. For example, this <strong>git diff <\/strong>usage identifies the differences in a specific file:<\/p><pre class=\"wp-block-preformatted\">git diff file1.txt<\/pre><p>To compare changes between two commits, use the following:<\/p><pre class=\"wp-block-preformatted\">git diff commit1 commit2<\/pre><p><strong>git rm<\/strong><\/p><p>This command removes files from your working directory and stages the removal for the next commit.<\/p><pre class=\"wp-block-preformatted\">git rm file1.txt<\/pre><p><strong>git mv<\/strong><\/p><p>Use this command to rename and move files within your working directory. Here&rsquo;s the Git command to rename a file:<\/p><pre class=\"wp-block-preformatted\">git mv file1.txt file2.txt<\/pre><p>To move a file to a different directory, enter:<\/p><pre class=\"wp-block-preformatted\">git mv file1.txt new_directory\/<\/pre><p><strong>git config<\/strong><\/p><p>This command configures various aspects of Git, including user information and preferences. For example, enter this command to set your email address for commits:<\/p><pre class=\"wp-block-preformatted\">git config --global user.email \"your.email@example.com\"<\/pre><p>The <strong>&ndash;global<\/strong> flag applies the configurations universally, impacting your local repository.<\/p><h3 class=\"wp-block-heading\" id=\"h-git-branching-and-merging-commands\">Git Branching and Merging Commands<\/h3><p><strong>git branch<\/strong><\/p><p>Use this command to <a href=\"\/uk\/tutorials\/how-to-use-git-branches\/\">manage branches in your Git repository<\/a>. Here&rsquo;s the basic <strong>git branch<\/strong> usage to list all existing branches:<\/p><pre class=\"wp-block-preformatted\">git branch<\/pre><p>To create a Git branch named &ldquo;feature&rdquo;, use:<\/p><pre class=\"wp-block-preformatted\">git branch feature<\/pre><p>To <a href=\"\/uk\/tutorials\/how-to-rename-a-git-branch\/\">rename a Git branch<\/a>, enter this command:<\/p><pre class=\"wp-block-preformatted\">git branch -m branch-name new-branch-name<\/pre><p><strong>git checkout<\/strong><\/p><p>This command lets you switch between branches and restore files from different commits.<\/p><p>The following is a <strong>git checkout <\/strong>usage to switch to an existing branch:<\/p><pre class=\"wp-block-preformatted\">git checkout branch_name<\/pre><p>To discard changes to a specific file and revert it to the last commit, use:<\/p><pre class=\"wp-block-preformatted\">git checkout -- file_name<\/pre><p><strong>git merge<\/strong><\/p><p>To combine a feature or topic branch into the main Git branch, use this command. Below is an example of the basic <strong>git merge <\/strong>usage:<\/p><pre class=\"wp-block-preformatted\">git merge branch_name<\/pre><p><strong>git cherry-pick<\/strong><\/p><p>This command allows you to apply specific commits from one branch to another without merging an entire branch.<\/p><pre class=\"wp-block-preformatted\">git cherry-pick commit_hash<\/pre><p><strong>git rebase<\/strong><\/p><p>This command is used to apply changes from one Git branch to another by moving or combining commits. It helps maintain a cleaner commit history:<\/p><pre class=\"wp-block-preformatted\">git rebase main<\/pre><p><strong>git tag<\/strong><\/p><p>This command marks specific points in your Git history, such as v1.0 or v2.0:<\/p><pre class=\"wp-block-preformatted\">git tag v1.0<\/pre><h3 class=\"wp-block-heading\" id=\"h-git-remote-repository-commands\">Git Remote Repository Commands<\/h3><p><strong>git clone<\/strong><\/p><p>This command creates a copy of a remote repository on your local machine. A basic <strong>git clone <\/strong>usage is to clone a repository from <a href=\"\/uk\/tutorials\/what-is-github\">GitHub<\/a>:<\/p><pre class=\"wp-block-preformatted\">git clone https:\/\/github.com\/username\/my-project.git<\/pre><p><strong>git push<\/strong><\/p><p>This command sends your local Git branch commits to a remote repository, updating it with your latest changes.<\/p><p>For example, you want to push changes from the local repository called &ldquo;main&rdquo; to the remote repository named &ldquo;origin&rdquo;:<\/p><pre class=\"wp-block-preformatted\">git push origin main<\/pre><p><strong>git pull<\/strong><\/p><p>This command fetches and integrates changes from a remote repository into your current local branch. Here&rsquo;s a <strong>git pull <\/strong>usage example to pull changes from the master branch:<\/p><pre class=\"wp-block-preformatted\">git pull origin master<\/pre><p><strong>git fetch<\/strong><\/p><p>To retrieve new commits from a remote repository without automatically merging them into your current branch, use this command:<\/p><pre class=\"wp-block-preformatted\">git fetch origin<\/pre><p><strong>git remote<\/strong><\/p><p>This command manages remote repositories associated with your local repository. The basic <strong>git remote <\/strong>usage lists the remote repository:<\/p><pre class=\"wp-block-preformatted\">git remote<\/pre><p>To add a new remote repository, specify its name and URL. For example:<\/p><pre class=\"wp-block-preformatted\">git remote add origin https:\/\/github.com\/username\/origin.git<\/pre><p><strong>git submodule<\/strong><\/p><p>This command is used to manage separate repositories embedded within a Git repository.<\/p><p>To add a submodule to your main repository, use:<\/p><pre class=\"wp-block-preformatted\">git submodule add https:\/\/github.com\/username\/submodule-repo.git path\/to\/submodule<\/pre><h3 class=\"wp-block-heading\" id=\"h-advanced-git-commands\">Advanced Git Commands<\/h3><p><strong>git reset<\/strong><\/p><p>This command is for undoing changes and manipulating the commit history. Here&rsquo;s a basic <strong>git reset <\/strong>usage example to unstage changes:<\/p><pre class=\"wp-block-preformatted\">git reset file1.txt<\/pre><p><strong>git stash<\/strong><\/p><p>To store temporary changes that are not yet ready to be committed, use this command:<\/p><pre class=\"wp-block-preformatted\">git stash<\/pre><p>To see a list of stashes:<\/p><pre class=\"wp-block-preformatted\">git stash list<\/pre><p>To apply the most recent stash and remove it from the stash list:<\/p><pre class=\"wp-block-preformatted\">git stash pop<\/pre><p><strong>git bisect<\/strong><\/p><p>This command is primarily used for identifying bugs or issues in your project&rsquo;s history. To start the bisecting process, use this command:<\/p><pre class=\"wp-block-preformatted\">git bisect start<\/pre><p>Git will automatically navigate you through commits to find the problematic ones using the following:<\/p><pre class=\"wp-block-preformatted\">git bisect run &lt;test-script&gt;<\/pre><p><strong>git blame<\/strong><\/p><p>This command determines the author and the most recent change to each file line:<\/p><pre class=\"wp-block-preformatted\">git blame file1.txt<\/pre><p><strong>git reflog<\/strong><\/p><p>This command logs Git branch changes. It allows you to track your repository&rsquo;s timeline, even when commits are deleted or lost:<\/p><pre class=\"wp-block-preformatted\">git reflog<\/pre><p><strong>git clean<\/strong><\/p><p>Last but not least, this command removes untracked files from your working directory, resulting in a clean and organized repository:<\/p><pre class=\"wp-block-preformatted\">git clean [options]<\/pre><p>The <strong>[options]<\/strong> can be customized based on your specific needs, such as <strong>-n<\/strong> for a dry run, <strong>-f<\/strong> for force, or <strong>-d<\/strong> for directories.<\/p><h2 class=\"wp-block-heading\" id=\"h-basic-git-commands-cheat-sheet\">Basic Git Commands Cheat Sheet<\/h2><p>If you&rsquo;re just starting out, it can be hard to remember even the essential Git commands. Lucky for you, we&rsquo;ve created a Git cheat sheet to help you master this tool. Download it or print it out to always have it ready when you&rsquo;re stuck remembering Git commands.<\/p><p class=\"has-text-align-center\"><a href=\"https:\/\/cdn.hostinger.com\/tutorials\/pdf\/Git-Cheat-Sheet-EN.pdf\" target=\"_blank\" rel=\"noopener\">Download complete GIT cheat sheet<\/a><\/p><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>We have explored a wide range of Git commands essential for effective source control management and seamless code collaboration. Whether you&rsquo;re an experienced developer or just starting out, mastering these Git commands will elevate your coding journey.<\/p><p>So, apply this knowledge and continue refining your Git commands skill. Good luck!<\/p><h2 class=\"wp-block-heading\" id=\"h-basic-git-commands-faq\">Basic Git Commands FAQ<\/h2><div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1693904678255\"><h3 class=\"schema-faq-question\">What Are the Most Used Git Commands?<\/h3> <p class=\"schema-faq-answer\">There are hundreds of Git commands. Some of the frequently used ones are <strong>git config<\/strong>,<strong> git clone<\/strong>,<strong> git init<\/strong>,<strong> git status<\/strong>,<strong> git push<\/strong>,<strong> git add<\/strong>,<strong> git commit<\/strong>,<strong> git branch<\/strong>,<strong> git pull<\/strong>,<strong> git merge<\/strong>,<strong> <\/strong>and <strong>git stash<\/strong>.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1693904695929\"><h3 class=\"schema-faq-question\">How to Get Started With Git?<\/h3> <p class=\"schema-faq-answer\">Installing Git on your operating system involves downloading its installer from its official website. Next, configure your name and email address using <strong>git config<\/strong>. Then, create a new repository with <strong>git init<\/strong> and begin adding and committing files using <strong>git add<\/strong>.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1693904705317\"><h3 class=\"schema-faq-question\">Can Git Commands Be Customized?<\/h3> <p class=\"schema-faq-answer\">Yes, customizing Git commands allows you to tailor the version control system to your specific workflows and preferences. You can personalize user information, default behaviors, aliases, and more using the <strong>git config<\/strong> command.<\/p> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Using Git commands when developing software or applications helps you simplify repository management and workflow. Whether you&rsquo;re an individual developer or a team member, learning Git simplifies version control and enhances effectiveness. This Git commands tutorial offers a list of Git commands along with their purposes and usage examples. It covers the basics and introduces [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/uk\/tutorials\/basic-git-commands\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":411,"featured_media":93346,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"Git Commands for Repository Management + Free Cheat Sheet","rank_math_description":"Starting out with Git? Check out this article and learn popular Git commands and a Git cheat sheet every developer should know.","rank_math_focus_keyword":"git commands","footnotes":""},"categories":[22642,22640],"tags":[],"class_list":["post-757","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pre-installed-applications","category-vps"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/basic-git-commands","default":0},{"locale":"pt-BR","link":"https:\/\/www.hostinger.com\/br\/tutoriais\/comandos-git","default":0},{"locale":"fr-FR","link":"https:\/\/www.hostinger.com\/fr\/tutoriels\/commandes-git","default":0},{"locale":"es-ES","link":"https:\/\/www.hostinger.com\/es\/tutoriales\/comandos-de-git","default":0},{"locale":"id-ID","link":"https:\/\/www.hostinger.com\/id\/tutorial\/perintah-git-dasar","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/basic-git-commands","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/basic-git-commands","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/basic-git-commands","default":0},{"locale":"es-MX","link":"https:\/\/www.hostinger.com\/mx\/tutoriales\/comandos-de-git","default":0},{"locale":"es-CO","link":"https:\/\/www.hostinger.com\/co\/tutoriales\/joomla-vs-wordpress-cms-utilizar-17","default":0},{"locale":"es-AR","link":"https:\/\/www.hostinger.com\/ar\/tutoriales\/comandos-de-git","default":0},{"locale":"pt-PT","link":"https:\/\/www.hostinger.com\/pt\/tutoriais\/comandos-git","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/basic-git-commands","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/basic-git-commands","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/basic-git-commands","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/basic-git-commands","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts\/757","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/users\/411"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/comments?post=757"}],"version-history":[{"count":29,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts\/757\/revisions"}],"predecessor-version":[{"id":123396,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts\/757\/revisions\/123396"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/media\/93346"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/media?parent=757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/categories?post=757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/tags?post=757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}