This tutorial will introduce you to a very popular branching strategy, scaled trunk-based development. This is a simple branching strategy with a main source of truth, the master
branch, and many short-lived "feature" branches.
The idea is that, when you want to make a change to your code base, you do so on a small, dedicated "feature" branch. You push that branch up to your remote repository, open up a pull request for peer review, merge it into master
, and then delete it. Then, to do another piece of work, you pull
the latest version of master
, and create another "feature" branch.
Note: You can copy and paste the commands into your terminal, omitting the $
. The $
is simply used to imply that this is a command run on the terminal, since many terminals use $
to signify where you can begin typing a command.
If you have not used git and GitHub before, you may need to sign up for a GitHub account. Follow these instructions to get set up.
-
Create a fork of the repository to save to your own GitHub projects
-
Clone your copy of the remote repository:
$ git clone https://github.com/<your username>/learning_git.git
- Change your current directory to the repository:
$ cd learning_git
-
To list the contents of the repository, you can run
$ ls -a
-
To read the contents of the files in the repository, run:
$ cat hello_erin.txt
$ cat hello_david.txt
- Create a new git branch:
$ git checkout -b hello_marlena
A note about branch names: feature branch names should reflect the change they introduce to the code base. They should start with a verb, like "adds", for example adds-mailing-feature
A note about the checkout
command: it can also be used to checkout existing branches. Try this out by switching back to master with $ git checkout master
and then back to your feature branch with $ git checkout -
(the -
symbol takes you back to the most recent place you were).
- Create a new file:
$ touch hello_marlena.txt
-
Run
$ git status
to see the untracked file -
Run
$ git add .
to check the new file into source control
Note: the .
essentially means "everything in the current directory", but you could also do $ git add hello_marlena.txt
if you don't want to check in any other changes.
-
Open the file in your preferred text editor
-
Add some content to the file and save it
-
Run
$ git status
to see the untracked changes -
Run
$ git diff
to see the changes -
Run
$ git add .
to include the file -
Run
$ git status
to see that there are uncomitted changes -
You can run
$ git diff --cached
to see the changes again, to verify them before pushing them up to the remote repository -
Run
$ git commit -m "adds hello_marlena.txt"
to commit the change
A note about commit messages: like feature branches, commit messages should describe the change they introduce, and usually start with a verb, for example "Adds zipcode to user"
-
Run
$ git push --set-upstream origin hello_marlena
to push the local branch to GitHub (remote) -
Open a pull request on GitHub and request a review
-
Once reviewed, merge the branch into
master
-
Delete the remote branch on GitHub
-
$ git checkout master
-
$ git pull
$ git pull
helps make sure you have the latest changes from the remote branch on your local copy of the branch
To see a list of commits to a branch, run $ git log
It's good practice to delete old local branches.
$ git branch
will show you the names of all branches you have locally. To delete one, run $ git branch -D <branch name>
Rebasing is common and important when working on a feature branch for more than a day, while other developers on your team are checking code into master
. I recommend rebasing your feature branch every day that you are working on it, and before merging it into master.
$ git checkout master
$ git pull
$ git checkout <feature branch name>
$ git fetch origin master
$ git rebase master
You may have merge conflicts, if someone has committed a change to master
in a file that you have also changed on your branch. See this article on resolving merge conflicts.
You may realize, after pushing a commit, that you need to make a very small change like fixing a typo. Rather than creating another commit just for that typo, you may want to simply amend your previous commit. To add a change to a previous commit, after checking in the change with $ git add .
, you can run:
$ git commit --amend
This will open your commit message in vim. To exit, enter :wq!
After doing something like amending or resetting a commit, you will have to force push those changes to master
. You can do so via $ git push -f
.
Sometimes, you may want to reset a commit or a number of commits. I like to do this when my branch is ready to merge, so that I don't merge a whole bunch of commits into master
. For example, I may have made small, incremental commits to my feature branch that will not be useful in master
's history. Instead, I can reset my commits like this:
$ git reset --soft HEAD~<number of commits to reset>
$ git add .
$ git commit -m "Message that makes more sense for master's history"
$ git push -f
Tagging is commonly done during releases to tag a certain version of the code. Tags differ from commits or branches, because commits and branches can be changed. Tags, however, are like a snapshot of the code and cannot be altered, so they make good, immutable historical records.