Skip to content

Instantly share code, notes, and snippets.

@blackfalcon
Last active November 5, 2024 19:53
Show Gist options
  • Save blackfalcon/8428401 to your computer and use it in GitHub Desktop.
Save blackfalcon/8428401 to your computer and use it in GitHub Desktop.
Git basics - a general workflow

Git-workflow vs feature branching

When working with Git, there are two prevailing workflows are Git workflow and feature branches. IMHO, being more of a subscriber to continuous integration, I feel that the feature branch workflow is better suited, and the focus of this article.

If you are new to Git and Git-workflows, I suggest reading the atlassian.com Git Workflow article in addition to this as there is more detail there than presented here.

I admit, using Bash in the command line with the standard configuration leaves a bit to be desired when it comes to awareness of state. A tool that I suggest using follows these instructions on setting up GIT Bash autocompletion. This tool will assist you to better visualize the state of a branch in regards to changes and being in sync with the remote repo.

Basic branching

When working in a decentralized workflow, the concepts can be simple. main represents the official history and should always deployable. With each new scope of work, aka feature, a developer creates a new branch. For clarity, make sure to use descriptive names like transaction-fail-message or github-oauth for your branches.

Although you may have a feature like 'user login and registration`, it may not be appropriate to create a feature branch at this level, there is too much work to be done. It may better to break these large deliverables down to smaller bits of work that can be continuously integrated into the project. Remember, commit early and often.

That being said, there are times when a single branch will be needed to deliver a large feature or prepare for a release. In this scenario, I'd suggest reading over the Git Commit Guidelines created by the Angular team at Google. As they put it, it is a series of "... precise rules over how our git commit messages can be formatted". As of late, I make heavy use of these commit message guidelines in all the projects I am working on.

Before you create a branch, always be sure you have all the upstream changes from the origin/main branch.

Make sure you are on main

Before I pull, I make sure I am on the right branch. I have GIT Bash autocompletion installed, so this is clear from the prompt. Otherwise, the following command is good to know to list out the branches I have locally as well as designate which branch I am currently on.

$ git branch

The checked out branch will have a * before the name. If the return designates anything other then main then switch to main

$ git checkout main

Once on main and ready to pull updates, I use the following:

$ git pull origin main

Depending on your setup, you may even be able to run only the following:

$ git pull

The git pull command combines two other commands, git fetch and git merge. When doing a fetch the resulting commits are stored as a remote branch allowing you to review the changes before merging. Merging, on the other hand, can involve additional steps and flags in the command, but more on that later. For now, I'll stick with git pull.

Now that I am all up to date with the remote repo, I'll create a branch. For efficiency, I will use the following:

$ git checkout -b my-new-feature-branch

This command will create a new branch from main as well as check out out that new branch at the same time. Doing a git branch here again will list out the branches in my local repo and place a * before the branch that is checked out.

  main
* my-new-feature-branch

Do you have to be on main to branch from main?

No. There is a command that allows me to create a new branch from any other branch while having checked out yet another branch. WAT?!

$ git checkout -b transaction-fail-message main

In that example, say I was in branch github-oauth and I needed to create a new branch and then check out the new branch? By adding main at the end of that command, Git will create a new branch from main and then move me (check out) to that new branch.

This is a nice command, but make sure you understand what you are doing before you do this. Creating bad branches can cause a real headache when trying to merge back into main.

Some files should be ignored

In any Git project, you will find a .gitignore file. This is a simple registry file where you can list the files you DO NOT want to commit to the repo. This would be any files that contain secret information like keys and tokens or any sensitive server configurations.

To read more about the basics of a .gitignore file, read ignoring files from the Github site.

The only thing I want to mention is if a resource has already been added to Git's control, adding it later to the .gitignore file will not ignore that file. The thing to remember here is that the intention is to remove the file from Git control, but not from disk. To do this, use the following command:

$ git rm --cached <filename>

For example, say we had a directory with the filed index.html and then the file config.yml, but the config file had a lot of secret info and it was accidentally added to the Git version control.

In the .gitignore file, first we would add config.yml then in the command prompt, run the following command:

$ git rm --cached config.yml

After running the command, you should see that config.yml is still in the directory, but running the following command, you would see that it is NOT being tracked by Git:

$ git ls-tree -r main --name-only

Branch management

When I am working on my new feature branch, it is a good idea to commit often. This allows me to move forward without fear that if something goes wrong, or you have to back out for some reason, I don't lose too much work. Think of committing like that save button habit you have so well programed into you.

Each commit also tells a little bit about what I just worked on. That's important when other devs on the team are reviewing my code. It's better to have more commits with messages that explain the step versus one large commit that glosses over important details.

Commit your code

As I am creating changes in my project, these are all unseated updates. With each commit there most likely will be additions, and there will also be deletions from time to time. To get a baring of the updates I have made, let's get the status.

$ git status

This command will give you a list of all the updated, added and deleted files.

To add files, I can add them individually or I can add all at once. From the root of the project I can use:

$ git add .

In order to remove deleted files from the version control, I can again either remove individually or from the root address them all like so:

$ git add -u

I'm lazy, I don't want to think, so I make heavy use of the following command to address all additions and deletions.

$ git add --all

All the preceding commands will stage the updates for commitment. If I run a git status at this point, I will see my updates presented differently, typically under the heading of Changes to be committed:. At this point, the changes are only staged and not yet committed to the branch. The next step is to commit with a message. Here is where I lean on the Angular style commit messages linked to above. To commit, do the following:

$ git commit -m "<type>(<scope>): <subject>"

It is considered best to illustrate your comment in the tense that this will do something to the code. It didn't do something in the past and it won't do something in the future. The commit is doing something now.

A bad subject example would be:

$ git commit -m "fixed bug with login feature"

A good subject example would be:

$ git commit -m "fix: update app config to address login bug" 

Comments are cheap. For more on how to write expressive commit messages, read 5 Useful Tips For A Better Commit Message.

Push your branch

When working with feature branches on a team, it is typically not appropriate to merge your own code into main. Although this is up to your team as to best manage, the norm is usually to make pull requests. Pull requests require that you push your branch to the remote repo.

To push the new feature branch to the remote repo, simply do the following:

$ git push origin my-new-feature-branch

As far as Git is concerned, there is no real difference between main and a feature branch. So, all the identical Git features apply.

Pro-tip: Use an alias in your ~/.gitconfig to make this easier. With the following alias, a user can simply run $ git publish and the CLI will take care of the rest.

[alias]
  publish = "!git push origin $(git branch-name)"

My branch was rejected?

There is a special case when working on a team and the feature branch being pushed is out of sync with the remote. There are two ways to to address this. A very common approach is to pull.

$ git pull origin my-new-feature-branch

This will fetch and merge any changes on the remote repo into the local feature branch with all the changes addressing any issues with diffs in the branch's history, now allowing you to push.

But there is a cost. This merge down will create a broken path in your project's history. A preferred method is to rebase. Read below for additional information on this.

Working on remote feature branches

When I am creating the feature branch, this is all pretty simple. But when I need to work on a co-workers branch, there are a few additional steps that I follow.

Tracking remote branches

My local .git/ directory will, of course, manage all my local branches, but my local repo is not always aware of any remote branches. To see what knowledge my local branch has of the remote branch index, adding the -r flag to git branch will return a list.

$ git branch -r

To keep my local repo 100% in sync with deleted remote branches, I make use of this command:

$ git fetch -p

The -p or --prune flag, after fetching, will remove any remote-tracking branches which no longer exist.

Switching to a new remote feature branch

Doing a git pull or git fetch will update my local repo's index of remote branches. As long as co-workers have pushed their branch, my local repo will have knowledge of that feature branch.

By doing a git branch you will see a list of your local branches. By doing a git branch -r you will see a list of remote branches.

The process of making a remote branch a local branch to work on is easy, simply check out the branch.

$ git checkout new-remote-feature-branch

This command will pull its knowledge of the remote branch and create a local instance to work on.

Keeping current with the main branch

Depending on how long you have been working with your feature branch and how large your dev team is, the main branch of your project may be really out of sync from where you created your feature branch.

When you have completed your update and prior to creating a pull request, you not only have to be up to date in order to merge your new code but also be confident that your code will still work with the latest version of main.

It's here where there are two very different schools of thought. Some teams don't mind if you PULL the latest version from main, by simply doing the following.

$ git pull origin main

This will fetch and merge any changes on the remote repo into the local branch with all the changes, now allowing your feature branch able to be merged. This works for the purpose of merging, but it's kind of gross on the branch's history graph.

Then there are teams who are not a fan of this process, simply because pulling from origin can really screw up the feature branch's history and make it harder to perform more advanced Git features if needed. So, in these situations, it's best to REBASE O_O.

Rebasing a feature branch is not as scary as most make it seem. All a rebase really is, is taking the updates of the feature branch and moving them to a new spot in the history as to be on top of the latest version of main. It's as if you just created that feature branch and made the updates. This creates a very consistent branch history that is easy to follow and easy to work within all situations.

To rebase your local feature branch off of the latest version of main, following these steps will be a guarantee every time.

$ git checkout main         /* ensure you are on the main branch
$ git pull                                   /* pull the latest from the remote 
$ git checkout my-feature-branch      /* checkout the feature branch
$ git push origin my-feature-branch  /* update your copy in the repo
$ git rebase main              /* rebase on the main branch
$ git push origin my-feature-branch --force   /* force update the remote

And that's it. This process will ensure that you have the latest version of main then take the commits from your feature branch, temporarily unset them, move to the newest head of the main branch and then re-commit them. As long as there are no conflicts, there should be no issues.

Force push? But ...

Yes, there are those who are not fans of force pushing, but in the case of a feature branch, this is ok. Now force pushing to main, well, that's a really bad idea.

When performing operations like rebasing you are in effect changing the branch's history. When you do this, you can't simply push to the remote as the histories are in conflict, so you will get rejected. To address this, adding the --force flag to force push tells the remote to ignore that error and replace the previous history with the new one you just created.

Conflicts

In the worst-case scenario, there is a conflict. This would happen even if you did the pull directly from main into the feature branch. To resolve a conflict, read the error report in the command prompt. This will tell you what file has a conflict and where.

When opening the file you will see a deliberate break in the file's content and two parts that are essentially duplicated, but slightly different. This is the conflict. Pick one of the versions and be sure to delete all the conflict syntax injected into the document.

Once the conflict(s) is/are resolved, back in the command line you need to add this correction.

$ git add .

Once the new updates are staged, you can't commit again as this process is inside a previous commit. So you need to continue with the rebase, like so:

$ git rebase --continue

If for any reason you get stuck inside a rebase that you simply can't make sense of, you can abort the rebase;

$ git rebase --abort

As long as there are no other issues, the rebase will continue and then complete with an output of the updates.

The Pull Request

The pull request is where the rubber meets the road. As stated previously, one of the key points of the feature branch workflow is that the developer who wrote the code does not merge the code with main until there has been through a peer review. Leveraging Github's pull request features, once you have completed the feature branch and pushed it to the repo, there will be an option to review the diff and create a pull request.

In essence, a pull request is a notification of the new code in an experience that allows a peer developer to review the individual updates within the context of the update. For example, if the update was on line 18 of header.html, then you will only see header.html and a few lines before and after line 18.

This experience also allows the peer reviewer to place a comment on any line within the update that will be communicated back to the editor of origin. This review experience really allows everyone on the team to be actively involved in each update.

Once the reviewer has approved the editor's updates, the next step is to merge the code. While it used to be preferred to merge locally and push main, Git has really grown into this feature and I would argue today it is most preferred to simply use the GUI took in Github.

Now the question is, how to merge? Github has three options;

  1. Create a merge commit
  2. Squash and merge
  3. Rebase and merge

Creating a merge commit is ok, this will simply merge in the new feature branch code into the main branch as long as there are no conflicts. Github will not allow you to merge if it already knows there will be conflicts.

The squash and merge process is interesting as this will compact all the commits to this feature branch into a single commit to the main branch. This may or may not be an issue depending on how your team want's to preserve history. If you are a user of the Angular commits, you may not want to use this feature.

Lastly, there is the rebase and merge. Showing my preference for rebasing earlier, I am definitely a fan of this merge action. This will do exactly what I explained earlier. It will take all the commits of the feature branch and reset them to the latest head of the main branch. If you did a rebase on the feature branch prior to creating a pull request, this process will be seamless and in the end, the most healthy for the project's history.

Shortcuts using aliases

There are some steps in there that we should just be doing all the time. What about making a single command alias that will cycle through all these commands just so we know things are always in good shape? Yup, we can do that.

In Bash

Using Git and Bash is like using a zipper and pants. They just go together. Creating a Bash alias is extremely simple. From your Terminal, enter

$ open ~/.bash_profile

This will open a hidden file in a default text editor. If you have a shortcut command for opening in an editor like Sublime Text or VS Code, use that to open the file. In the file add the following:

alias refresh="git checkout main && dskil && git pull && git fetch -p"

The alias dskil is useful for removing annoying .DS_Store files. You should have a .gitignore file that keeps these out of version control, but I like to keep a clean house too. To make that work, add the following:

alias dskil="find . -name '*.DS_Store' -type f -delete"

With this in your .bash_profile, you simply need to enter refresh in the command line and POW!

In Powershell

If you are on Windows and using Powershell, you can use the same aliases, but the set up is different. The article Windows PowerShell Aliases is a good tutorial of the process.

Summary

Following this simple workflow has helped keep my projects clean and Git hell free. I hope it serves you well too.

But ... the story doesn't end here. Well, the post does, but the story does not. There are many more advanced features that your team can use. Outside of that, for more in-depth learning on Git, I invite you to read the Git book, it's free and contains awesome learning.

@Omar-Suliman
Copy link

thank you so much

@Jyothireddyb
Copy link

I couldnot get what a pullrequest is???

@Vanshika-Mittal
Copy link

How do I tie my GitBash to my GitHub account? Also, can you tell me where my GitBash commits go if its not tied to my account.

@eylulyurdakul
Copy link

This is a super useful article with very clear explanations, thank you!

@rtsuh
Copy link

rtsuh commented Oct 17, 2018

Very helpful - thank you!

@kvedantmahajan
Copy link

You just documented, what I would have documented myself. You haven't missed a point. In fact learnt a couple of things more. --no-ff and alias features.

@gsamaras
Copy link

There is a typo in that command: $ git push origin my-new-feature-brach

Fixed it, OP can merge from here: https://gist.github.com/gsamaras/0d0b3e40100a6ff7fd68a7cbbb6d6439

@dflourusso
Copy link

Great article, thank you.

@bbneo
Copy link

bbneo commented Dec 8, 2018

Thank you for this. If you are just working on a project by yourself and just want to use GitHub as a central synchronization for your work (pulling from one place (e.g. work) and then pushing any changes right back to GitHub and then pulling it from another place (e.g., home) and doing the same)
do you need to worry about making branches?

@jandrebu
Copy link

Great and thanks!

@gemcdermott
Copy link

Thank you. Great tutorial!

@sumitchinchansure
Copy link

Thank you so much for this Article..!!!
Explained very nicely

Copy link

ghost commented Jun 24, 2019

This is very helpful. :) Thank you!

@bhpayne
Copy link

bhpayne commented Aug 30, 2019

@SevCoelho
Copy link

typical Linux warped thinking - simplest of all source control operation , such as merge 2 versions of the same file , becomes convoluted to the extreme, they way the author savors the experience is asinine

Wow

@sherribooher
Copy link

Thank you. Well-written tutorial. And ignore the troll @aao13

@Weewolh
Copy link

Weewolh commented Mar 30, 2020

Thanks !

@rd67
Copy link

rd67 commented May 10, 2020

Thank you

@darrentmorgan
Copy link

This was really concise and helpful. Thankyou!

@vasanthsp64
Copy link

I know that If I want to merge the current branch to the master branch, I should do the following:
git checkout master
git merge mybranch

Is this the standard procedure or is there any better option?

@blackfalcon
Copy link
Author

I know that If I want to merge the current branch to the master branch, I should do the following:
git checkout master
git merge mybranch

Is this the standard procedure or is there any better option?

The process I go with is to be sure to rebase before the merge.

  1. $ git checkout main
  2. $ git pull origin main
  3. $ git checkout myBranch
  4. $ git rebase main
  5. $ git checkout main
  6. $ git merge myBranch

This is a manual process that mirrors https://github.blog/2016-09-26-rebase-and-merge-pull-requests/

@hsaissi
Copy link

hsaissi commented Dec 26, 2020

And that's it. This process will ensure that you have the latest version of master then take the commits from your feature branch, temporarily unset them, move to the newest head of the master branch and then re-commit them. As long as there are no conflicts, there should be no issues.

I'm not sure where the "temporarily unset them" part here fits in the chain of presented commands fit? Can anyone add some details about this?

@jimidak
Copy link

jimidak commented Jan 15, 2021

awesome!!

@theGaryLarson
Copy link

👍

@bistmadhav
Copy link

Great

@preritvishal
Copy link

Thank you very much, i needed to learn all this is simple terms.

@blackfalcon
Copy link
Author

I'm not sure where the "temporarily unset them" part here fits in the chain of presented commands fit? Can anyone add some details about this?

This happens in the background. The meaning behind this statement is that the commit is removed from the branch and reseated at the head of the branch.

@blackfalcon
Copy link
Author

I know that If I want to merge the current branch to the master branch, I should do the following:
git checkout master
git merge mybranch

Is this the standard procedure or is there any better option?

This is 100%, just make sure that my branch is rebased off of master/main before you merge. That way you ensure that there is a smooth linear merge. If not, there will be a weird step in-between asking for a commit that is generated by Git.

My process is

  1. Make sure main is up to date with the remote
  2. Checkout feature branch
  3. $ git rebase main
  4. Checkout main
  5. Merge feature branch

@Andriy-Lyt
Copy link

Andriy-Lyt commented Oct 29, 2021

When I need to add to the master branch the changes I made in my branch.
which of the following do I do and what would be the difference?

  1. git push origin master from my branch.
    or
  2. switch to master branch, run git merge my-branch-name.
    or
  3. while on master run git pull my-branch-name

Do the above commands perform similar operation?

@ITAYCO890
Copy link

Thanks
Super!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment