Git workflow
Published on
Table of content
- Name and email
- Init new repository
- Display the status of current branch
- Create new branch
- Change/Create origin of Branch
- List all local branches
- Change to a different local branch
- Add changes to next commit
- Commit your changes
- Push branch upstream
- Delete local branch
Git is a free open-source distributed version control system. Without it, collaboration becomes a problem. The reality is that if you want to become a developer of any kind, you'll have to master it. To install it simply follow the steps outlined on Git's website.
There are many tools to visualize and perform git commands via a GUI. I used the source control tab in VSCode for a number of years. That being said, GUIs are for soy devs. If you want to gain the respect of senior devs at your job, learn to use the command line!
Add your name and email
Git creates an immutable log of commits. Other than code, it saves information like a unique commit id, the timestamp and the identity of the author of the commit. Because of this, it wants your name and email.
Init new git repo
Whenever you are starting a new project. After cd'ing into the directory, the first step should be to initialize a new git repository.
At this point, git will start tracking all new changes in the directory. It also creates the main branch. In older versions, it was called the master branch but that triggered some people.
Display the status of current branch
It's easy to forget on which branch you are and what files were modified. Whenever you are in doubt, use git status
.
Create new branch
We don't develop directly on main. Let's create a new branch instead with branch
.
Just be vigilant from which branch you are creating this one. You'll usually want to switch to main before creating a branch.
Change/Create origin of branch
You might have a repo and want to change the origin.
List all of the local branches
You'll soon start create a bunch of different branches and forget their names. Whenever you want the list,
branch
is the answer.
The output is weird here, you'll have to type q
to exit.
Change branches
Switch to branches with checkout
.
Just make sure that all current changes are either stashed or commited.
Add changes
You need to select which changes you want to include in the next commit. To do this, you add changes in the
staging area with add
.
Commit your changes
Once all changes are in the staging area, it's time to create the commit. Commits needs a message for future git archeology which will inevitably happen.
Push feature branch on github for the first time
You created a new branch, commited stuff on it. Now you want to push on github, the standard git push
will not work.
You'll get an error saying "fatal: The current branch branch_name has no upstream branch". This one always gets me.
This simply means that the branch that you have created locally does not exist on github. You need to create the branch on github.
From there you will see your new branch on Github.
Delete local branches
Once your finished with a local branch and push it on github, it will live on your machine.