{"id":16107014,"url":"https://github.com/mikeesto/git-branches","last_synced_at":"2025-04-06T03:29:35.510Z","repository":{"id":115471291,"uuid":"117490472","full_name":"mikeesto/git-branches","owner":"mikeesto","description":"A friendly \u0026 introductory guide to using Git branches","archived":false,"fork":false,"pushed_at":"2018-01-15T03:09:24.000Z","size":8,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-12T09:57:16.609Z","etag":null,"topics":["branches","git"],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mikeesto.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-01-15T03:03:06.000Z","updated_at":"2018-06-30T00:54:25.000Z","dependencies_parsed_at":null,"dependency_job_id":"5c30ab3c-67bc-4f07-8573-ffe85289c140","html_url":"https://github.com/mikeesto/git-branches","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikeesto%2Fgit-branches","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikeesto%2Fgit-branches/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikeesto%2Fgit-branches/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikeesto%2Fgit-branches/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mikeesto","download_url":"https://codeload.github.com/mikeesto/git-branches/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247430843,"owners_count":20937873,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["branches","git"],"created_at":"2024-10-09T19:14:55.169Z","updated_at":"2025-04-06T03:29:35.493Z","avatar_url":"https://github.com/mikeesto.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# :octocat: Git branches\n\n## Why do we need branches?\n\nFor a moment, pretend that you are working on a website for a small business. Your boss comes to see you. He asks you to redesign the navigation section of the website. You tell him it will take four days. He nods, trudges off and you begin coding.\n\nAt the end of your second day working on the redesign, you receive an email. It's from your boss and he has an urgent request. The business desperately needs to hire a new employee. Your boss wants a short job advertisement placed on the website's home page - right now!\n\nIf you had been working on the redesign without using branches, you now have a problem. The code in the website repository contains an uncompleted navigation section that is going to take you another two days to complete. \n\nHow would you handle this situation? Would you carefully try to cut out the uncompleted navigation code and then add it back in later? Or did you make a backup copy of the files prior to working on the navigation section? Perhaps you consider stashing away your navigation commits and downloading a fresh copy of the files from an external repository?\n\nAll of these are messy, time consuming and reactive solutions. How could you have avoided getting into this situation in the first place? The answer, as I'm sure you've guessed, is branches.\n\nOne of the great benefits of branches is that it allows us as developers the ability to separate code that we are working on from code that is \"complete\" (or at the very least \"currently in use\"). Branches may at times seem unnecessary when working on a personal project, but they play a vital role when working with code that is used and accessed by others.\n\n## How to use branches\n\nFor this demo we are going to need a new git repository. As a quick refresher, you can do this by creating a new folder, opening your terminal, navigating to that folder and then typing:\n\n```\ngit init\n```\n\nLet's also add something to our repository. Create a text file in the repository, and on the first line of the text file write: \n\n```\nprint \"Hello from the master branch\"\n```\n\nSave the file and close it. Let's also commit this change by typing into the terminal:\n\n```\ngit add -A\ngit commit -m 'added a new text file'\n```\n\n#### Viewing the current branch\n\nWe can see a list of all the available branches by typing:\n\n```\ngit branch\n```\n\nYou should see that the only branch currently available is the master branch. This is the default branch. The asterix indicates that this is the branch currently in use.\n\n#### Creating a new branch\n\nTo create a new branch, simply type `git branch` and then the name of the branch. For our example that means:\n\n```\ngit branch navigation\n```\n\nTo switch to this new branch: \n\n```\ngit checkout navigation\n```\n\nYou can complete these two steps in one line by taking advantage of the `-b` flag:\n\n```\ngit checkout -b navigation\n```\n\nAt this point we are now using the navigation branch. It is exactly the same as the master branch because we haven't yet added anything to it.\n\nLet's run `git branch` again.\n\nThere are now two branches listed: master and navigation, and we are on the navigation branch.\n\n#### Making a change to the navigation branch\n\nOpen up the text file and on the second line add:\n\n```\nprint \"Hello from the navigation branch\"\n```\n\nLet's also commit this change to the navigation branch:\n\n```\ngit add -A\ngit commit -m 'added hello from nav branch'\n```\n\nNow our navigation branch is ahead of our master branch by 1 commit. Until we tell git to merge our navigation branch back into the master branch, the code in the master branch remains unchanged. \n\n#### Swapping back to the master branch\n\nWe can swap back to the master branch by typing: \n\n`git checkout master`\n\nAt this point, reopen the text file. As expected, in the master branch we can only see the first line of the text file. This makes sense because only the first line was commited to the master branch. \n\nClose the text file.\n\n#### Merging the master and navigation branches\n\nFinally, let's see how we can merge the two branches together. We are currently in the master branch and we will merge the navigation branch into it.\n\n```\ngit merge navigation\n```\n\nReopen the text file. Now that the branches have been merged, both lines in the text file should be visible.\n\n\u003cdiv style=\"text-align:center\"\u003e\n\u003cimg src=\"images/branches.png?raw=true\"\u003e\n\u003c/div\u003e\n\nCongratulations, you now know how to create, swap and merge branches! :tada:","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikeesto%2Fgit-branches","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmikeesto%2Fgit-branches","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikeesto%2Fgit-branches/lists"}