7 Git commands essential for contributing with a team
Tags: git, whatimlearning, branches, commits
Published: 2020-10-18 01:24:40

---
title: 7 Git commands essential for contributing with a team
published: true
date: 2020-10-07 19:41:47 UTC
tags: git,WhatImlearning,branches,commits
canonical_url: https://gabbysadventure.com/7-git-commands-essential-for-contributing-with-a-team/
---
![git-logo-e1602099771632](https://dev-to-uploads.s3.amazonaws.com/i/sjt3uoevjegw2x6qbct2.jpg)
 
 
<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Add repo folder to local computer</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":2197,"sizeSlug":"large","className":"is-style-default"} -->
<figure class="wp-block-image size-large is-style-default"><img src="https://gabbysadventure.com/wp-content/uploads/2020/10/fork-1024x375.jpg" alt="Where to find Fork button in top right of repository" class="wp-image-2197"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
&nbsp;
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Under the Code tab, click the green Code button and copy the HTTPS repo link.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":2199,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://gabbysadventure.com/wp-content/uploads/2020/10/clone-1024x375.jpg" alt="Where Code button is to copy HTTPS link" class="wp-image-2199"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>In the terminal, cd to your where you want your folder to live, whether Desktop or Documents or another folder.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

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

<!-- wp:paragraph -->
<p>This has not only copied the folder but it also connected your local terminal to the GitHub repo</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Now cd into your new created folder.</p>
<!-- /wp:paragraph -->

```bash
cd repo-name
```

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

```bash
npm update
```

<!-- wp:paragraph -->
<p>This will automatically update all dependencies and will take some time to complete since dependency folders, like npm modules, are quite large.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You are all set. Now just double check it works by opening the file in a browser (possibly with npm start).</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Create a new branch</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

```bash
git checkout -b new-branch-name
```

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

```bash
git push -u origin new-branch-name
```

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Change branches</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To see what branch you are on as well as see all other options for other branches, type this.</p>
<!-- /wp:paragraph -->

```bash
git branch -a
```

<!-- wp:paragraph -->
<p>Your active branch will have an * next to it. The others are branch options you have. It should look something like this.</p>
<!-- /wp:paragraph -->

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

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

```bash
git checkout main
```

<!-- wp:paragraph -->
<p>You'll get a confirmation that you switched branches and you can check it by doing <code>git branch -a</code> again.</p>
<!-- /wp:paragraph -->

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

<!-- wp:paragraph -->
<p>Great! Now this was just an example, but remember to switch back to your branch with <code>git checkout new-branch-name</code> to continue working with your project.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Track File Changes</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

```bash
git status
```

<!-- wp:paragraph -->
<p>This will show all the changes you made in red because that means these changes aren't being tracked yet.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Most of the time you'll want to commit all the files so you can just do this to add and track all changes.</p>
<!-- /wp:paragraph -->

```bash
git add .
```

<!-- wp:paragraph -->
<p>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 .</p>
<!-- /wp:paragraph -->

```bash
git add file1 file4 file5
```

<!-- wp:paragraph -->
<p>Now we should be able to do <code>git status</code> 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.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Commit</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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!</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><em>Note: Before hitting enter to commit, if you worked on this commit with a teammate, read the next section first on adding multiple authors.</em></p>
<!-- /wp:paragraph -->

```bash
git commit -m "Commit message here."
```

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

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

<!-- wp:heading {"level":4} -->
<h4>Add multiple authors to a commit</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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 <em><strong>make sure the email is one they have linked to their GitHub account</strong></em> or else their info will look a bit funky in the official GitHub commit. Notice again that the commit quotes don't close until <em><strong>after</strong></em> the authors are added.</p>
<!-- /wp:paragraph -->

```bash
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>”
```

<!-- wp:paragraph -->
<p>If you did this using Github desktop, you can simply add the exact format, <code>Co-authored-by: name &lt;name@example.com&gt;</code>,  in the "Detailed description" box under the short description box of the commit.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Push Changes</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

```bash
git push origin
```

<!-- wp:paragraph -->
<p><code>Origin</code> is saying, push to the branch I'm currently on and you don't have to fully type out <code>git push origin new-branch-name</code> every time.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Now go to Github, click your branch, and you can see your commits along with the co-authors you added.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Pull Latest Code</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><em>Note: Be sure to be in your desired branch or just the main one if you don't need a specific branch</em></p>
<!-- /wp:paragraph -->

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

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Conclusion</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->