Learn How to Use Git & GitHub: A Guide for Absolute Beginners
Published: 2023-09-05 07:51:29

Embarking on Your Coding Journey? You’ve probably heard of Git & GitHub. Dive in, and let’s demystify these essential tools for every developer.

## Why Git & GitHub?

Version control is paramount for collaborative projects and individual projects alike. Git helps you manage changes to your code, while GitHub provides an online platform to share, collaborate, and version-control your repositories. Together, they’re an unbeatable combination for any developer.

## Getting Started with Git

### Setting Up Your Identity

It’s essential Git knows who’s making changes. Let’s set that up:

1. Open your terminal or command prompt.
2. Enter:
   ```
   git config --global user.name "Your Name"
   git config --global user.email "your.email@example.com"
   ```
   > **Tip**: Ensure the email used is the same as your GitHub account for a seamless experience.

### Creating Your First Repository

Before diving deep, check if a repository exists in your current directory:
```
ls -a
```
If you spot a `.git` directory, a repository already exists!

Otherwise, initialize a new one with:
```
git init
```

## Syncing with GitHub

### Setting Up Your GitHub Repository

Head over to the [GitHub website](https://github.com/) and create a new repository.

### Connecting the Dots

Now, let’s link your local repository (on your machine) with the remote one on GitHub:
```
git remote add origin https://github.com/your_username/repository_name.git
```
Run into trouble? Don’t fret!

Check if a git repository already exists connected to the directory:
```
git remote -v
```
If that’s the case, you can:

- Modify the URL for origin:
  ```
  git remote set-url origin https://github.com/your_username/repository_name.git
  ```
- Or perhaps, you’d like to start afresh by removing origin and adding it again:
  ```
  git remote remove origin
  git remote add origin https://github.com/your_username/repository_name.git
  ```
- Alternatively, you can try a different name other than origin:
  ```
  git remote add another_name https://github.com/your_username/another_repository.git
  ```
  > **Tip**: Naming conventions can help streamline your workflow. Choose names that resonate with your project.

## Staying Updated with Git

### Tracking Changes

Add and commit changes within your repository. This helps Git understand and track your modifications:

- For all files:
  ```
  git add .
  ```
- For a specific file:
  ```
  git add file_name
  ```
- Commit the change with a descriptive message:
  ```
  git commit -m "Your descriptive message here"
  ```

### Updating GitHub

Every time you modify your code, it’s a good practice to push changes to GitHub:

- If it’s your first push:
  ```
  git push -u origin main
  ```
- For subsequent pushes:
  ```
  git push origin main
  ```

### Quick Cheat Sheet:

```
git add
```
or
```
git add file_name
```
and
```
git commit -m "descriptive message"
git push -u origin main
```

## Determined to Learn More?

Delve deeper into the world of GitHub with its [official documentation](https://docs.github.com/en).

### Extra Help:

If you’re just starting out and only making simple pushes on your own, you might want to check out the VisualGit script, which automates the processes described in this guide.

[You can find it here.](https://github.com/alexaldearroyo/visualgit/)