{"id":25831,"date":"2025-04-03T00:49:39","date_gmt":"2025-04-03T03:49:39","guid":{"rendered":"https:\/\/mastertrend.info\/?p=25831"},"modified":"2025-04-03T00:52:28","modified_gmt":"2025-04-03T03:52:28","slug":"10-basic-git-commands","status":"publish","type":"post","link":"https:\/\/mastertrend.info\/en\/10-comandos-basicos-de-git\/","title":{"rendered":"10 Basic Git Commands Every Developer Needs \ud83d\udda5\ufe0f"},"content":{"rendered":"<h2>10 Basic Git Commands to Protect Your Code \ud83d\udd12<\/h2>\n<p>Spending another all-nighter trying to recover lost code changes? You&#039;re not alone. That&#039;s why millions of developers rely on Git, the world&#039;s leading version control system, to track every change and protect their work. Here&#039;s a rundown of the commands you&#039;ll use most. \ud83d\ude80<\/p>\n<p>Si sos nuevo en Git, empecemos con una refrescada. Un repositorio de Git (o <a class=\"wpil_keyword_link\" href=\"https:\/\/mastertrend.info\/en\/ai-and-automation-agents\/\" target=\"_blank\"  rel=\"noopener\" title=\"AI agents and automation: Save time and money now! \u2699\ufe0f\ud83d\ude80\" data-wpil-keyword-link=\"linked\"  data-wpil-monitor-id=\"34551\">repo<\/a> en corto) contiene todos los archivos del proyecto y toda la historia de revisiones. Un repo tiene commits, que son los que se usan para registrar los cambios en el repo, y cada commit tiene un breve mensaje que el usuario escribe para indicar qu\u00e9 cambios realiz\u00f3. Git tambi\u00e9n puede <a title=\"Guide to Disabling Performance Mode in Windows 11\" href=\"https:\/\/mastertrend.info\/en\/disable-performance-mode\/\" target=\"_blank\" rel=\"noopener\" data-wpil-monitor-id=\"5167\">help manage<\/a> conflicts (for example, if two people edit the same line of code) before merging. <a href=\"https:\/\/git-scm.com\/downloads\/win\" target=\"_blank\" rel=\"noopener\" data-schema-attribute=\"mentions\">To learn more about installing Git on Windows, click here.<\/a><\/p>\n<h2 id=\"to-clone-an-existing-repo\"><span class=\"item-num\">1 <\/span> Clone an Existing Repo<\/h2>\n<p>The first command we can start with is <strong>git clone<\/strong>, which is a command that connects and <a title=\"\u2728 How to blur the background of photos in Windows 11 and surprise your friends.\" href=\"https:\/\/mastertrend.info\/en\/how-to-blur-the-background-of-photos-in-windows-11\/\" target=\"_blank\" rel=\"noopener\" data-wpil-monitor-id=\"5168\">download a copy<\/a> from an existing repository to your local machine. Usually, the existing repository is located remotely, such as on GitHub or GitLab.<\/p>\n<p>First, go to a repo and click the green dropdown menu that says \u201cCode,\u201d then the copy to clipboard icon next to the GitHub repository URL, which will clone it using the <a title=\"Google Chrome: Trick to instantly reveal hidden URLs\" href=\"https:\/\/mastertrend.info\/en\/trick-to-reveal-urls\/\" target=\"_blank\" rel=\"noopener\" data-wpil-monitor-id=\"5166\">Web URL<\/a>. This is the easiest method and clones using HTTPS:<\/p>\n<figure>    <img decoding=\"async\" style=\"height: auto;max-width: 100%\" src=\"https:\/\/mastertrend.info\/wp-content\/uploads\/2024\/11\/10-Comandos-Basicos-de-Git-para-Empezar.png\" alt=\"Number of arrows showing the option to clone repositories over HTTPS on GitHub.\" width=\"1454\" height=\"812\" title=\"\"> <\/figure>\n<p>Then, run the following command with the URL you just copied:<\/p>\n<pre><code class=\"hljs php\">git <span class=\"hljs-keyword\">clone<\/span> https:<\/code><\/pre>\n<figure>    <img decoding=\"async\" style=\"height: auto;max-width: 100%\" src=\"https:\/\/mastertrend.info\/wp-content\/uploads\/2024\/11\/1731397110_892_10-Comandos-Basicos-de-Git-para-Empezar.png\" alt=\"Repo clone completed message in Git Bash CLI.\" width=\"878\" height=\"519\" title=\"\"> <\/figure>\n<p>Once the repo is cloned, you should have a local copy of it on your machine. \ud83d\udc4d<\/p>\n<section class=\"emaki-custom-block emaki-custom-note\">If you get an error saying &quot;fatal: repository not found,&quot; check the URL. If it&#039;s a private repo, you may need permissions to access it.<\/section>\n<h2 id=\"to-create-a-new-repo\"><span class=\"item-num\">2 <\/span> Create a New Repo<\/h2>\n<p>If you want to create a new Git repository instead of cloning an existing one, run <strong>git init<\/strong>This initializes the repository in the specified directory, giving it a path. So it&#039;s ideal for new or untracked projects that you want to start using Git.<\/p>\n<p>First, make sure you are in the correct folder before running the command:<\/p>\n<pre><code class=\"hljs\">git init<\/code><\/pre>\n<figure>    <img decoding=\"async\" style=\"height: auto;max-width: 100%\" src=\"https:\/\/mastertrend.info\/wp-content\/uploads\/2024\/11\/1731397111_252_10-Comandos-Basicos-de-Git-para-Empezar.png\" alt=\"Empty repo error message in Git init commands.\" width=\"879\" height=\"518\" title=\"\"> <\/figure>\n<h2 id=\"creating-a-branch-for-collaboration\"><span class=\"item-num\">3 <\/span> Create a Branch to Collaborate<\/h2>\n<p>A branch in Git is a version of your repository, so multiple people can work on it simultaneously. In other words, it&#039;s an independent line of development within a repo. Typically, there are multiple branches in a repo.<\/p>\n<p>To create a local branch, run the following command:<\/p>\n<pre><code class=\"hljs\">git branch branch-name<\/code><\/pre>\n<p>To list all your branches, run:<\/p>\n<pre><code class=\"hljs\">git branch<\/code><\/pre>\n<p>To delete a branch:<\/p>\n<pre><code class=\"hljs\">git branch -d branch-name<\/code><\/pre>\n<section class=\"emaki-custom-block emaki-custom-tip\">When you delete a branch, it is sometimes necessary to force the deletion. You just have to capitalize the <strong>-D<\/strong>, So: <strong>git branch -D branch-name<\/strong><\/section>\n<h2 id=\"switch-between-branches\"><span class=\"item-num\">4 <\/span> Switching between Branches<\/h2>\n<p>The command <strong>git checkout<\/strong> It is one of the most used, mainly to switch between branches, but it can also be used to review files and commits.<\/p>\n<p>To switch between branches and check them out in your local directory:<\/p>\n<pre><code class=\"hljs xml\">git checkout branch-name\r\n<\/code><\/pre>\n<p>For newer versions of git, you can run:<\/p>\n<pre><code class=\"hljs javascript\">git <span class=\"hljs-keyword\">switch<\/span> branch-name<\/code><\/pre>\n<p>For the above commands to work, the branch you are switching to must exist locally, and any changes to your current branch must be committed or saved first.<\/p>\n<section class=\"emaki-custom-block emaki-custom-tip\">Shortcut command to create and switch branches at the same time: <strong>git checkout -b branch-name<\/strong><\/section>\n<h2 id=\"check-git-status\"><span class=\"item-num\">5 <\/span> Check Git Status<\/h2>\n<p>This is another common command, which can tell you different information about the current branch, such as whether the current branch is up to date or not, if there is anything left to commit or push, and if there are any files that were modified or deleted.<\/p>\n<pre><code class=\"hljs\">git status<\/code><\/pre>\n<p>This is what the output should look like if there are no changes to be made:<\/p>\n<figure>    <img decoding=\"async\" style=\"height: auto;max-width: 100%\" src=\"https:\/\/mastertrend.info\/wp-content\/uploads\/2024\/11\/1731397111_826_10-Comandos-Basicos-de-Git-para-Empezar.png\" alt=\"Git status command on the command line with output saying nothing to commit, clean working tree.\" width=\"877\" height=\"516\" title=\"\"> <\/figure>\n<h2 id=\"commit-sets-of-changes\"><span class=\"item-num\">6 <\/span> Commit Change Sets<\/h2>\n<p>This may be the most used Git command. When we&#039;re ready to save our work, perhaps after a specific task or issue, we can use <strong>git commit<\/strong>This essentially captures a snapshot of the changes currently being prepared in the project.<\/p>\n<p>You also need to write a short, clear commit message so you and other developers know about the changes. Don&#039;t forget to surround it with quotation marks.<\/p>\n<pre><code class=\"hljs sql\">git <span class=\"hljs-keyword\">commit<\/span> -m <span class=\"hljs-string\">&quot;confirmation message&quot;<\/span><\/code><\/pre>\n<section class=\"emaki-custom-block emaki-custom-warning\"><strong>Git commit<\/strong> Just save your changes locally. You still need to push them to a remote repo.<\/section>\n<h2 id=\"rolling-back-changes\"><span class=\"item-num\">7 <\/span> Undo Changes<\/h2>\n<p>The command <strong>git revert<\/strong> allows you <a title=\"C\u00f3mo restablecer Windows 10\/11 a trav\u00e9s de CMD\" href=\"https:\/\/mastertrend.info\/en\/how-to-reset-windows-10-11-via-cmd\/\" target=\"_blank\" rel=\"noopener\" data-wpil-monitor-id=\"5164\">eliminate<\/a> all the changes a single commit has made to your local repo. For example, if a previous commit added a file called ReadMe.md to the repo, a <strong>git revert<\/strong> In that commit, the ReadMe.md will be removed from the repo. A new commit will also be created to reflect this change.<\/p>\n<p>All you need to do is run <strong>git revert<\/strong> followed by the commit ID:<\/p>\n<pre><code class=\"hljs sql\">git revert <span class=\"hljs-keyword\">commit<\/span>-<span class=\"hljs-keyword\">id<\/span><\/code><\/pre>\n<p>If you&#039;ve made a lot of commits and you&#039;re not sure where the commit ID is, you can identify the commit by running the command <strong>git log<\/strong>. Copy the commit ID and run the command <strong>git log<\/strong> with the commit ID.<\/p>\n<figure>    <img decoding=\"async\" style=\"height: auto;max-width: 100%\" src=\"https:\/\/mastertrend.info\/wp-content\/uploads\/2024\/11\/1731397111_213_10-Comandos-Basicos-de-Git-para-Empezar.png\" alt=\"Git log command in CLI showing previous commits and commit IDs.\" width=\"873\" height=\"703\" title=\"\"> <\/figure>\n<section class=\"emaki-custom-block emaki-custom-warning\">Do not confuse <strong>git revert<\/strong> with <strong>git reset<\/strong>The latter will undo every change that occurred since a given commit and change the commit&#039;s history. This isn&#039;t ideal if other people are working on the same branch.<\/section>\n<h2 id=\"upload-all-your-local-changes\"><span class=\"item-num\">8 <\/span> Upload All Your Local Changes<\/h2>\n<p>Once you&#039;ve finished making all your changes and committing them, you&#039;ll want to push your local changes to the remote repo. Pushing is the act of transferring these changes and commits from your local machine to the remote repository. You can specify which branch you want to send the changes to.<\/p>\n<pre><code class=\"hljs\">git push origin master<\/code><\/pre>\n<p>The above command pushes the changes to the master branch (master is usually considered the main branch, but &quot;main&quot; is also commonly used). If <strong>master<\/strong> doesn&#039;t work, try with <strong>main<\/strong>.<\/p>\n<section class=\"emaki-custom-block emaki-custom-tip\">It is recommended to run <strong>git status<\/strong> before uploading your changes.<\/section>\n<h2 id=\"retrieve-all-changes\"><span class=\"item-num\">9 <\/span> Recover All Changes<\/h2>\n<p>This is a command I use when I return to a project and need to retrieve all the new changes made to the master branch (whether through my merge or from other developers) that exist remotely. In other words, it&#039;s a command you use when you want to get updates from the remote repository.<\/p>\n<pre><code class=\"hljs\">git pull origin main<\/code><\/pre>\n<p>As before, yes <strong>master<\/strong> doesn&#039;t work, try with <strong>main<\/strong>. Since this command combines the functions of <strong>git fetch<\/strong> and <strong>git merge<\/strong>, instantly applies the latest modifications to your local repository (<strong>git merge<\/strong>) after retrieving updates from the remote repository (<strong>git fetch<\/strong>). You can learn more about pull requests in Git.<\/p>\n<h2 id=\"merge-it-all-together\"><span class=\"item-num\">10 <\/span> Merge It All Together<\/h2>\n<p>Finally, once you&#039;re done working on your branch and everything is working correctly, the last step is to merge the branch into the main branch (usually dev or master, but check the repo).<\/p>\n<p>You can do this by running the command <strong>git merge<\/strong>. First you should <a title=\"Running an AI model on Xbox 360: Amazing \ud83d\udc7e\" href=\"https:\/\/mastertrend.info\/en\/running-an-ai-model-on-xbox-360\/\" target=\"_blank\" rel=\"noopener\" data-wpil-monitor-id=\"5165\">execute <strong>git fetch<\/strong> to update your branch<\/a> local, and then make your merge:<\/p>\n<pre><code class=\"hljs sql\">git <span class=\"hljs-keyword\">merge<\/span> branch-name<\/code><\/pre>\n<section class=\"emaki-custom-block emaki-custom-note\">Make sure you are on the branch you want to merge into your remote master branch.<\/section>\n<hr \/>\n<p>In the end, learning Git is like riding a bike: once you start, it only gets easier with every push! \ud83d\udeb4\u200d\u2642\ufe0f\ud83d\udcbb<\/p>","protected":false},"excerpt":{"rendered":"<p>10 Basic Git Commands Master these commands and avoid losing your code easily \ud83d\udca1\ud83d\ude80<\/p>","protected":false},"author":1,"featured_media":25832,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ai_generated_summary":"","iawp_total_views":47,"jnews-multi-image_gallery":[],"jnews_single_post":[],"jnews_primary_category":[],"jnews_social_meta":[],"jnews_review":[],"enable_review":"","type":"","name":"","summary":"","brand":"","sku":"","good":[],"bad":[],"score_override":"","override_value":"","rating":[],"price":[],"jnews_override_counter":[],"footnotes":""},"categories":[1435],"tags":[1445,1558,1581],"class_list":["post-25831","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutoriales","tag-evergreencontent","tag-techtips","tag-windowstips"],"_links":{"self":[{"href":"https:\/\/mastertrend.info\/en\/wp-json\/wp\/v2\/posts\/25831","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mastertrend.info\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mastertrend.info\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mastertrend.info\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mastertrend.info\/en\/wp-json\/wp\/v2\/comments?post=25831"}],"version-history":[{"count":15,"href":"https:\/\/mastertrend.info\/en\/wp-json\/wp\/v2\/posts\/25831\/revisions"}],"predecessor-version":[{"id":105574,"href":"https:\/\/mastertrend.info\/en\/wp-json\/wp\/v2\/posts\/25831\/revisions\/105574"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mastertrend.info\/en\/wp-json\/wp\/v2\/media\/25832"}],"wp:attachment":[{"href":"https:\/\/mastertrend.info\/en\/wp-json\/wp\/v2\/media?parent=25831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mastertrend.info\/en\/wp-json\/wp\/v2\/categories?post=25831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mastertrend.info\/en\/wp-json\/wp\/v2\/tags?post=25831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}