7 Git commands essential for contributing with a team

You’ve been coding away, working on your own projects, adding to your own repositories but now you’re working with a team of people and you realize, “I have no idea how to work with a team on a project”. Let’s learn the git commands that will help you transition to this new team workflow so that you can continue confidently.

Add repo folder to local computer

If you’re not already a member of the repository, Fork it in the upper right corner so it goes to your repos. If you are already apart of the repo you can skip this step.

Where to find Fork button in top right of repository

Under the Code tab, click the green Code button and copy the HTTPS repo link.

Where Code button is to copy HTTPS link

In the terminal, cd to your where you want your folder to live, whether Desktop or Documents or another folder.

Now let’s add the repo to your computer by doing git clone and pasting that link you just copied. Then hit enter to let it do it’s thing.

git clone https://github.com/profile-name/repo-name.git

This has not only copied the folder but it also connected your local terminal to the GitHub repo

Now cd into your new created folder.

cd repo-name

If you’re working with a project that requires dependencies such as node modules, we will need to update those to their latest version. Chances are good they have become out of date while sitting in the original repo.

npm update

This will automatically update all dependencies and will take some time to complete since dependency folders, like npm modules, are quite large.

You are all set. Now just double check it works by opening the file in a browser (possibly with npm start).

Create a new branch

We don’t want to commit everything to the main branch immediately because we might break something, so a branch needs to be created that we can work off of.

If multiple people are working on the same issue, one branch can be used, but each new issue needs a new branch ideally. To create a new branch do this below.

git checkout -b new-branch-name

Now this branch is on your local computer, so how do your teammates get access to this branch? We have to push it to the online repo.

git push -u origin new-branch-name

You can confirm it worked by going to Github where you can see that the group repo has a new branch and you can click “branches” to see your’s and all the others.

Change branches

Now ate this point, you should be working on this new branch you created. How can you confirm this? If it’s wrong, how can you change it?

To see what branch you are on as well as see all other options for other branches, type this.

git branch -a

Your active branch will have an * next to it. The others are branch options you have. It should look something like this.

* new-branch-name
main
remotes/origin/new-brach-name
remotes/origin/main

What if that asterisk was on the main branch? Or what if you needed to go to the main branch for some reason? You can easily switch between them. Let’s switch from new-branch-name to the main branch.

git checkout main

You’ll get a confirmation that you switched branches and you can check it by doing git branch -a again.

* main
new-branch-name
remotes/origin/new-brach-name
remotes/origin/main

Great! Now this was just an example, but remember to switch back to your branch with git checkout new-branch-name to continue working with your project.

Track File Changes

Now you’ve been working and saving your project as normal and you’re ready to start committing, but first we need to make sure we commit the right changes.

git status

This will show all the changes you made in red because that means these changes aren’t being tracked yet.

Most of the time you’ll want to commit all the files so you can just do this to add and track all changes.

git add .

But what if you have a couple of files that don’t need to be committed? Maybe you had a personal file for your own purposes of unofficial debugging or testing .

git add file1 file4 file5

Now we should be able to do git status again and if you did all, they should all be green now. If you did certain files, only those files should be green. Green means they are being tracked and ready to be committed.

Commit

Now we have to commit these changes you made locally along with a message describing what you did. Luckily this is very simple to do!

Note: Before hitting enter to commit, if you worked on this commit with a teammate, read the next section first on adding multiple authors.

git commit -m "Commit message here."

If you want a multi-line message, you can simply hit enter. As long as you don’t close the quotes, it will keep going as the commit message.

git commit -m "Commit message here.
Second commit line.
Third commit line."

Add multiple authors to a commit

Now when you commit, Github will give you credit as the committer and author. But you’re working with a team, so what if that one commit was a group effort? Let’s give everyone credit for it! We’re going to add a line or so to the commit message.

There’s a feature called “Co-authored-by” that allows you to connect each member’s name and GitHub account to the commit. The name isn’t too important but make sure the email is one they have linked to their GitHub account or else their info will look a bit funky in the official GitHub commit. Notice again that the commit quotes don’t close until after the authors are added.

git commit -m “Commit message here.
*skip this line*
*skip this line*
Co-authored-by: name <name@example.com>
Co-authored-by: name <name@example.com>”

If you did this using Github desktop, you can simply add the exact format, Co-authored-by: name <name@example.com>, in the “Detailed description” box under the short description box of the commit.

You’ve officially committed your code to your local new-branch-name, but now let’s add these commits to the group new-branch-name online.

Push Changes

Pushing is a very simple task that simply takes all the commits and changes you made locally to the group repository. You should still be in your new-branch-name but double check with git branch -a. Good? Great! Now let’s push.

git push origin

Origin is saying, push to the branch I’m currently on and you don’t have to fully type out git push origin new-branch-name every time.

Now go to Github, click your branch, and you can see your commits along with the co-authors you added.

Create a pull request via the GitHub desktop to request merging this branch with the main branch. Wait for approval or review or whatever your team does then the head merger will merge it.

Pull Latest Code

The code on the main repository has updated since you last used it and you need to make some new changes. You don’t need to fork and clone anymore because it’s on your computer already, just not the updated one. Let’s pull the new code.

Note: Be sure to be in your desired branch or just the main one if you don’t need a specific branch

git pull origin
//if new dependencies were added by another contributor or you are not sure, do...
npm install
//if it's been awhile or you know an update occurred, do...
npm update

You can now check your local folder and you should notice the latest code in there. You can work on this as usual and follow all the same previous steps from above.

Conclusion

You now have all the basic knowledge you need to effectively contribute to a group project! And just in time for Hacktoberfest 😉 .Hopefully this has helped someone to become 1 step closer to becoming a better developer.

Leave a Reply