DEV Community

Cover image for Git minimum for effective project development
JetThoughts Dev for JetThoughts

Posted on • Edited on • Originally published at jetthoughts.com

Git minimum for effective project development

For beginners who are not familiar with Git or GitFlow, we grabbed the most common commands to be effective from the first day.

Global configurations

Initialize and configure the Git to push the current branch to a branch with the same name on the remote repo. While pushing the code, if branch with your local name does not exist in your remote repo, Git will automatically create it:

    git init
    git config --global push.default current
Enter fullscreen mode Exit fullscreen mode

For GitHub, we set up information about the author of commits, like this:

    git config --global user.name "Sergey Sviridov"
    git config --global user.email "sergey@users.noreply.github.com"
Enter fullscreen mode Exit fullscreen mode

Setup the project

Get the project to your machine:

    git clone git@github.com:jetthoughts/react-rails-5-starterkit.git
Enter fullscreen mode Exit fullscreen mode

Work with the new changes

If you are working in a team, before starting the new issue, refresh the state of your local master branch using the following commands:

    git checkout master
    git pull -p --rebase
Enter fullscreen mode Exit fullscreen mode

Then create a local branch:

    git checkout -b <branch-name>
Enter fullscreen mode Exit fullscreen mode

Or fetch the remote branch:

    git checkout <branch name>
Enter fullscreen mode Exit fullscreen mode

Store the changes

Once changes are made, we use:

    git add -p .
Enter fullscreen mode Exit fullscreen mode

This command begins an interactive staging session that would let us choose the portions of a file to add to the next commit. You can read about this option in the Git Guide.

Make sure all the changes are ready to be saved:

    git status
Enter fullscreen mode Exit fullscreen mode

When we are ready to create a commit:

    git commit -m "Your message"
Enter fullscreen mode Exit fullscreen mode

You can check on how to write good commit messages in the post of Chris Beams.

Sometimes we want to switch branches, but don’t want to commit what we’ve been working on yet, so we’ll stash the changes. To push a new stash into a stack:

    git stash
Enter fullscreen mode Exit fullscreen mode

We can apply newly stashed code by using the command:

    git stash pop
Enter fullscreen mode Exit fullscreen mode

Finally, for sending it to the remote repository:

    git push -u
Enter fullscreen mode Exit fullscreen mode

Keep your branch clean

Often the branch has one or several commit messages like “Fix typo” or “WIP”. They are not useful for git history and should be cleaned. One of the ways of solving is:

    git add .
    git commit --fixup <commit's SHA>
Enter fullscreen mode Exit fullscreen mode

commit’s SHA’ is SHA for commit, the new changes should be merged with. Finally, once all the changes were made, run:

    git rebase -i --autosquash origin/master
Enter fullscreen mode Exit fullscreen mode

It will automatically organize the merging of these ‘fixup’ commits and associated normal commits.

Finish the issue

Download new data from the remote repository and remove any remote-tracking branches which no longer exist within the remote repo:

    git fetch -p
Enter fullscreen mode Exit fullscreen mode

Once you’ve opened a pull request, and your code is commented by others, some fixes might be added. Usually, these changes are not a big deal and these small commits are interesting only while they are seen in a pull request so that the reviewer can check on what and when you’ve changed.

So, when you are ready to merge these commits, it becomes useful to squash them into one thoroughly described commit. It is needed to have a simple and clean history.

Rewrite history relative to origin/master:

    git rebase -i origin/master
Enter fullscreen mode Exit fullscreen mode

As a result, we’ll get the list of our commits and the list of actions to be done further. In our team, we use ‘squash’. We leave the first commit immutable with the command ‘pick’, and, as for the others, we rewrite them with the command ‘squash’, and then save and close. Git stuck the commits and suggests to enter commit-message, then saves and closes it.

If we found an error after the rebase action, there is a command that will give us a full history of what happened within the head of our branches. Run it, then find the line which refers to the state you want to get back into:

    git reflog
    git reset --hard <SOME-COMMIT-ID>
Enter fullscreen mode Exit fullscreen mode

After rebasing, our local branch differs from the branch on the server. To fix this, we need to push it with the force option:

    git push -uf
Enter fullscreen mode Exit fullscreen mode

Rare but must know

While working on a branch with partners, someone can push code with force option. Our changes are not actual and we need to set our branch to exactly match the remote branch:

    git fetch origin
    git reset --hard origin/<branch_name>
Enter fullscreen mode Exit fullscreen mode

To find out the author’s last modified line of a file one by one:

    git blame
Enter fullscreen mode Exit fullscreen mode

For hotfixes, we can use the ‘cherry-pick’. Make sure you are on the master branch and get the commit you need to be fixed from the production branch:

    git cherry-pick <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Do the fixes and merge it to the release branch.

In order to form the new release:

    git merge --no-ff origin/master
Enter fullscreen mode Exit fullscreen mode

The — no-ff option ensures that a fast-forward merge will not happen and that a new commit object will always be created. It allows you to keep the moment of adding the new feature in commits’ history.

To get detailed changes info per file for each commit:

    git log -p
Enter fullscreen mode Exit fullscreen mode

Summary

There are a lot of useful Git commands. And we can reach the goal in different ways using different commands. This article is showing our own way polished by the knowledge of our teammates. Feel free to find the approach that will suit your needs. Good luck in gitting :)!

Sergey Sviridov is a Software Engineer at JetThoughts. Follow him on LinkedIn or GitHub.

If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories.

Top comments (0)