Short guide working with branches in Git & GitHub
I consider myself a beginner working with Git and GitHub, so this steps is a good start (or practice) to work in a new feature or experiments in any GitHub repository.
💡 Before you start, previously you should have clone the repository where you will be working on. For this example the repository name used is hello-world
Check whether a branch exists in the repository
git branch -a # Output (something like this): * main remotes/origin/HEAD -> origin/main remotes/origin/main remotes/origin/readme-edits
For this example there is only the main branch and it is important to check if there is a previously branch created.
Create the new branch
git checkout -b hello-world_new_feature # Output (something like this): Switched to a new branch 'hello-world_new_feature'
💡 In case that the branch name exists, the git command will show you a message error
Make the changes in the new branch
Now you can modify the files, add, delete and so on in this new branch, for this example I only modified the content of the README.md file with the following lines:
# hello-world Hello Everyone! This repo is just to practice.
Stage and commit the new changes
Stage the changes and check the branch status:
# Stage changes git add README.md # Check the status git status # Output (something like this): On branch hello-world_new_feature Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: README.md
Commit the changes:
git commit -m "README.md file modified" # Output (something like this): [hello-world_new_feature e7b0423] README.md file modified 1 file changed, 1 insertion(+), 3 deletions(-)
Merge the changes
Now if everything works fine, you have to switch to the master or main branch, to do this run the following command:
git checkout main # In this example, the 'master' branch is main # Output (something like this): Switched to branch 'main' Your branch is up to date with 'origin/main'. Once you are in the **master** or **main branch**, just run the **merge** command: git merge hello-world_new_feature --no-ff # Output (something like this): Merge made by the 'recursive' strategy. README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
💡 The –no-ff command part, retains the commit messages
The last step is push the changes to GitHub
git push # Output (something like this): Enumerating objects: 6, done. Counting objects: 100% (6/6), done. Delta compression using up to 4 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (4/4), 421 bytes | 140.00 KiB/s, done. Total 4 (delta 1), reused 0 (delta 0) remote: Resolving deltas: 100% (1/1), done. To https://github.com/<your username>/hello-world.git 2ec5ed4..8c3979e main -> main
You should see the final changes in your main or master branch.
When you are working in a project (in a company, freelance or even in an open source project, and so on), it is very important to follow the best practices to maintain stable the original source code, so this steps described above is a little piece how you could work safe and keep the original source code without any risk for unintentional changes.
Thanks for reading! TM