My Daily Git Workflow

All git commands you need to know to handle any project from the start to end

Matthew Wong
Level Up Coding

--

Photo by Yancy Min on Unsplash

Git is our good friend when developing ourselves or collaborating with our colleagues. It’s hard to imagine without versioning. But, learning git commands can be a nightmare to beginners as there are so many git commands to handle different situations. I’ve been through this process.

Let me share my daily git workflow when working with real-world projects.

TL;DR Below is my daily git workflow to handle most of the projects:

My Daily Git Workflow

Setup

If you just install git on your own machine, you could setup a global git config file with your name and email so all the git projects on your machine could use the default git config.

git config --global user.name <YOUR_NAME>
git config --global user.email <YOUR_EMAIL>

With the above commands, .gitconfig file will be created in your home directory. To check you global git config, you could use:

git config --global -l

Start a working area

After setting up the global git config, you can really start a new working area.

If creates an empty repository or turn a directory into a git repository, you could use:

git init

Executing git init creates a .git subdirectory in the current working directory, which contains all of the necessary Git metadata for the new repository. For example, some subdirectories for objects and refs. A HEAD file is also created which points to the currently checked out commit.

If you want to work on an existing remote repository, you could use:

git clone

Internally, git clone first calls git init to create a new repository. It then copies the data from the existing repository, and checks out a new set of working files.

Before start making changes on a repository

Branching

A branch plays an important role in version control. It helps us to create an independent line of development and serves as an abstraction for the edit/stage/commit process. New commits are recorded in the history for the current branch, which results in a fork in the history of the project.

To list all branches including local and remote branches, you could use:

git branch -a

To list all local branches only, you could use:

git branch

To list all remote branches only, you could use:

git branch -r

After checking all the available branches, you could create a new branch or switch to another branch.

To create new branch, you could use:

git branch <BRANCH_NAME>

or

git checkout -b <BRANCH_NAME>

To switch to another branch, you have to create a local branch first to avoid a detached HEAD. You could:

git checkout -b <LOCAL_BRANCH_NAME> <REMOTE_BRANCH_NAME>

Now you can start developing and changing a bunch of files.

After creating and editing files

git creates a commit (very much like a patch file) based on the files you want added to commit.

First, you have to tell git which files are going to be committed. Any changes you have made to those files will be included in the commit. This is called staging.

To add all files including new untracked files, modified files, or deleted files to the Stage, you could use:

git add .

To all some files to the Stage only, you could use:

git add <FILE_NAME_1> <FILE_NAME_2> <FILE_NAME_3>

After adding files to the Stage, you can create a new commit with a commit message. You could use:

git commit -m "COMMIT_MESSAGE"

Before pushing the committed changes to the remote repository, you may want to inspect your current working repository.

To list which files are staged, unstaged, and untracked, you could use:

git status

It shows you what’s been going on with git add and git commit. Status messages also include relevant instructions for staging/unstaging files.

Collaboration

git is a version control system designed for coordinating work among programmers collaboratively. Your colleagues could be working on the same git repository at the same time. Most of the time you have to get back in sync with changes committed by others before pushing your changes to the remote repository.

To download contents from the remote repository, you could use:

git fetch

You can get all the updates from the remote repository, including new branches

If you found that there are some updates of the branch you are working on and you want to make sure your branch is in sync, you could use:

git pull

This will fetch all the changes from the remote repository and merge any remote changes in the current local branch.

To upload local repository content to a remote repository, you could use:

git push origin <BRANCH_NAME>

If you want to put a forked branch back to the master branch, you could use:

git checkout master
git pull
git checkout <BRANCH_NAME>
git merge master

Merge Conflicts

Sometimes, the working branch may conflict with the master branch while merging as your colleagues are also developing.

Here are the normal steps of resolving merge conflicts:

  1. Open the conflict files and modify them manually
  2. After modifying the file, you can use git add to stage the newly merged files
  3. Use git commit command to create new commit with message
  4. Use git push to upload local repository content to a remote repository

Basically you know all git commands which are sufficient to handle daily tasks.

To know more about my backend learning path, check out my journey here:

--

--