Working With Git for Beginners
Git is an extremely powerful and useful tool. It allows us to manage releases, develop features, fix bugs, and save our work with maximum efficiency. For this post, I thought I'd share my git workflow. I've adopted the gitflow workflow, so it may seem familiar to some of you. You can find the Gitflow guide from Atlassian here.
This post is written with mac users in mind, but other users may find it useful as well.
Setting Up Our Workflow
Before starting our workflow, we need to set up our branches. There are two main branches we need:
- develop
- master
To add these branches, navigate to your project directory using terminal.
cd [your project directory]
Then type the following two commands.
git checkout -b develop # Creates a new branch named develop
git checkout -b master # Creates a new branch named master
Type the following to list the branches you just created.
git branch # Lists all your git branches
Your branches will be listed like below. Any other branches you've created will also be listed here.
The develop branch name is somewhat self-explanatory. It's used to develop your project. Development takes place by merging your feature branches into develop. The master branch is used to store the state of our code that is released. This enables us to perform tasks like hotfixes (more on that later). You'll want to avoid doing development work in the master branch.
Branching and Merging Features
Now that our two main branches have been created it's time to get to work. Generally, we want to create a feature branch for this. While still in the project directory type:
git checkout develop # Change to your develop branch
git checkout -b myfeaturebranch # Create a new branch from the develop branch
Now that your feature branch has been created feel free to work away at whatever you need.
When you've finished developing your feature, it's time to merge your changes back into develop. Merging can be tricky. If you have any merge conflicts (when changes were made to the same pieces of code in both the branches being merged) I'll be writing a guide on how to resolve merge conflicts using mergetool on mac in the near future. Stay tuned.
To start your merge, type the following into terminal.
git commit -m "record your commit message here" # commit your changes
git checkout develop # Change to your develop branch
git merge myfeaturebranch # Merge myfeaturebranch changes into develop
You'll then be notified in terminal on whether the merge was successful. If it was successful, congratulations, you've successfully merged your branch.
Creating a Release and Pushing to Remote
For the following section, I'll assume you already have a remote repository set up. If you haven't added one yet, I recommend this guide for adding a remote repository on Github. Now that we've created a feature it's time to release it into the wild. There are a few steps to this:
Create a release branch
git checkout -b release[version#]
Next, perform any version changes in this branch, commit your changes and merge them to your develop and master branches
git commit -m "record your commit message here" # commit your changes
git checkout develop # Change to your develop branch
git merge release[version#] # Merge release[version#] changes into develop
git checkout master # Change to your master branch
git merge release[version#] # Merge release[version#] changes into develop
Then, push your branches to remote
git checkout develop # Changes to your develop branch
git pull # It's best practice to always pull your
git push # repository before you push
git checkout master # Change to your master branch
git pull # It's best practice to always pull your
git push # repository before you push
Finally, create and upload your build
git checkout master # Change to your master branch
# Create and upload you build from the master branch
Hotfixes
You've now pushed up a build and released it to the world. The very next day you receive feedback that an essential feature isn't working. You need to fix it yesterday. Because you uploaded your build from your master branch, you absolutely know that nothing has changed since that upload. This is where hotfixes come in. To create a hotfix we'll need to create a new branch from master, perform our bug fix on that branch and merge that branch back into master and develop. We then create a new build and release it. Here is the workflow below.
git checkout master # Change to your master branch
git checkout -b hotfixbranch # Create your hotfix branch
Perform your bug fix in the hotfixbranch branch and then merge back into your develop and master branches.
git checkout hotfixbranch # Change to your hotfix branch
git commit -m "record your commit message here" # commit your changes
git checkout master # Change to your master branch
git merge hotfixbranch # Merge the hotfix branch
git checkout develop # Change to your master branch
git merge hotfixbranch # Merge the hotfix branch
Push your changes to remote.
git checkout develop # Changes to your develop branch
git pull # It's best practice to always pull your
git push # repository before you push
git checkout master # Change to your master branch
git pull # It's best practice to always pull your
git push # repository before you push
Create and upload your bug fix.
git checkout master # Change to your master branch
# Create and upload you build from the master branch
Conclusion
Git is a wonderful tool. It allows for greater organization and efficiency when developing applications. It even allows for fast and reliable bug fixes. This guide explains how to use a basic git workflow assuming that merges happen flawlessly. For a guide on how to deal with merge conflicts, stay tuned!
I came to the (wrong) conclusion that it was yet another tool in the endless list of crap that I had to learn to be a developer. It turns out, however, that it’s one of the most important ones. https://redtube.onl/ https://beeg.onl/ https://spanktube.vip/spankbang/