How to Work with Git Day to Day

If you've landed here, I imagine it's your first day at a job where the team uses Git. You may not know what to do and are looking for a quick explanation of a common workflow—or maybe you're just exploring the internet. Either way, I can help. Let's get started.

Whether your team uses GitHub or GitLab doesn't matter for the basics: the workflow usually revolves around issues and pull requests (PRs). We'll focus on GitHub in this article.

Let's say you've received your first task, but you don't know how to create a branch and don't even have access to the project yet. That's okay—don't panic. Things will work out, or at least they usually do 😆

Installing Git

If Git is already installed, feel free to skip to the next section 🎉

Otherwise, run the following command to install Git on your machine:

Linux

In your terminal, run:

sudo apt-get install git-all

Windows

Download the Windows installer

Mac

Download the Mac installer

Setting up your Git identity for work

To check whether Git is configured with your work email, run this command in your terminal:

git config --list

If nothing appears, or if the email shown is different from your corporate email, configure your work identity with these commands:

git config --global user.name "Seu nome"
git config --global user.email seuemailcoorporativo@exemplo.com

Great, now we can move on.

Accessing the project

Make sure your SSH key is configured so you can access the project. There are plenty of tutorials online, so a quick search should help.

Once SSH is set up, open your project and look for a button or menu labeled Clone. It will offer two options:

  • SSH
  • HTTPS

Choose SSH and copy the URL it provides. That's the URL for the project where you'll start your first task.

Cloning the project

I recommend creating a folder called workspace for your projects. If another developer uses your machine later, they'll know where to find them. You can choose any name you prefer.

Open a terminal in that folder and run:

git clone url-que-você-copiou

When the command finishes, it will create a folder named after the project. You can now access the project on your machine :D

Updating the cloned project

With the project open, update your local copy so your main or master branch is current. The primary branch name depends on your team's convention. Check which one your team uses; on GitHub, you can see it near the Clone button. Here's an example:

Master branch

To get the latest work from your team, run:

git pull origin master

To fetch all branches used by your team, run:

git fetch origin

Creating a branch

To create a branch for your task (we're almost there!), run the following command.

Teams have their own conventions, so ask which prefixes they use for each type of task. Common examples are:

  • For a new feature, teams often use feature or feat.
  • For a bug fix, they use fix.
  • For a code update or maintenance task, they may use patch.

Let's assume you're building a new feature. You could name your branch feature/your-task-identifier:

git checkout -b feature/identificador-da-sua-tarefa

That's it: the branch has been created, and you're now working on it instead of the primary branch. (We never make changes directly on the primary branch.)

Committing your changes

Commits save changes to your branch.

To commit one file at a time, run:

git add caminho_do_arquivo
git commit -m "Adicionando novo componente"

That file is now committed. If you prefer to commit several files together, use:

git add .
git commit -m "Adicionando novo componente"

All changed files will now be included in a commit with the message “Adicionando novo componente”.

Pushing your changes to the remote repository on GitHub or GitLab

To push your branch and its commits, run:

git push origin nome-da-sua-branch

Your branch is now on the remote repository. Next, open a pull request or check that your commits are linked to the issue in GitLab.

If your team hosts projects on GitLab, including the issue number in the branch name—for example, feature/182-issue—can link your commits to the issue you're working on.

That's it! You can now work on your task with your team using Git. If you have questions or need help, you can reach me on Twitter @belrubim.

Buy Me A Coffee

Comentários