Git tip - one way to use branches

This is a popular git workflow. It won’t work for everyone, but it works for a lot of people.

When you do new work, do it on a a branch in your git repo.

When you’ve done a bit of work, commit it to your repo, and push it back to origin. Caution - this code will be visible to anyone, it is not private.

In github, make a “pull request” to eventually merge your branch back into master. The gibhub PR gives you a super easy way to see the changes you have made on your branch. after any push to origin, the GitHub UI will show a button to make this PR in a single button press.

When your branch is “done”, merge it back to master, and think seriously about doing a “squash merge”. Squash merge makes a single commit to master that has all your changes, rather than replaying every change that you made on your branch into master. In any case, it is easy to do either kind of merge from the GitHub UI.

Squash merge keeps your history a little “cleaner”, and it will reduce the size of your repo (and hence the time it takes to clone it). Disadvantages are the you don’t have an accurate record of exactly when each line of code was changed. More importantly there are some complex branching scenarios that become very difficult to merge if you use squash merges. Like if your branch master -> a -> b, and then squash merge a -> master it will be very difficult to merge b -> master.


Interesting, i might try to do that for my university works :slight_smile:
but i may need to re read a few times to fully understand :rofl:
Still thanks for sharing :wink:

1 Like

Without GitHub:

git checkout -b feature
# make some commits
git diff master
git checkout master
git merge feature

to squash:

git rebase -i master
git checkout master
git merge feature
git branch -d feature

Yeah, there are several unrelated things in this post. Sorry about that. Breaking it down I’d say that the basics of branching and merging are the most important thing.

Using the GitHub UI for comparing branches and other things can be easier than using the command line, but as @Vortico points out, you can of course do everything from the git command line.

Lastly, the difference between regular merge and squash merge is subtle and confusing - that choice can be pushed off - use regular merging until then.

1 Like