Initial introduction to Git and github

joel_hanerth

Joel Hanerth

Posted on September 19, 2024

Initial introduction to Git and github

If you're a developer or just starting to program, you've probably heard of Git and GitHub. These tools are essential in the world of software development, especially for those working in teams or on open-source projects. But what exactly are they, and why are they so important?

What is Git?

Git is a distributed version control system created by Linus Torvalds in 2005. It allows developers to track changes in code, collaborate on different features, and revert changes when necessary. Git makes team collaboration easier since each developer can have their local copy of the repository and integrate their contributions into the main project in a controlled manner.
Version control is also extremely useful for maintaining the project's history, enabling you to "go back in time" and see how the code evolved, or even revert to a previous state if something isn't working as expected.

What is GitHub?

GitHub is an online platform that hosts Git repositories. It adds a user-friendly interface and several extra features to collaborate with other developers. Besides storing repositories, GitHub offers tools such as pull requests, issues, and projects, making collaboration management and tracking bugs and new features easier.
Another important point is that GitHub has become one of the main platforms for open-source development, allowing developers worldwide to collaborate on a wide variety of projects.
With Git and GitHub, you not only have a powerful way to manage code locally but also a space to share your work and collaborate globally.

How to create a repository on GitHub:

1. Access GitHub: Log in to your GitHub account. If you don’t have an account, create one at github.com.

2. Create a new Repository:

  • In the upper-right corner of the screen, click the "+" icon and then click New repository.

    Image description

    Image description

  • Fill in the repository information:

    • Repository name: Give your repository a name (for example, "my-project").
    • Description (optional): Add a brief description of the project.
    • Choose whether you want the repository to be Public or Private.
  • You can check the option to add a README file if you want the repository to have an initial description.

    Image description

3. Create the Repository:

  • After filling in the details, click Create repository.
  • GitHub will create the repository and show the repository URL, which you will use to clone it into your Visual Studio. Image description

Cloning the repository in Visual Studio

Now that you've created the repository on GitHub, you can clone it to your local machine and start working on it.

Steps to clone a repository in the Visual Studio terminal:

  1. Open Visual Studio.
  2. Open the Integrated Terminal.
  3. Navigate to the desired folder.
  4. Use the git clone command:
    • In GitHub, copy the repository URL (HTTPS or SSH).
    • In Visual Studio's terminal, type the following command:
   git clone <repository_url>
Enter fullscreen mode Exit fullscreen mode

5.Result:

  • Git will clone the repository into the current folder, and you'll see something like this in the terminal:
   Cloning into 'my-project'...
   remote: Counting objects: 100, done.
   remote: Compressing objects: 100% (XXX/XXX), done.
   Receiving objects: 100% (XXX/XXX), done.
Enter fullscreen mode Exit fullscreen mode

6.Navigate to the Cloned Repository:

  • Now, go to the folder of the cloned repository:
   cd my-project
Enter fullscreen mode Exit fullscreen mode

YOU’RE READY TO START!

Note: If you haven’t previously configured Git on your machine, you won’t be able to clone the repository successfully. Before trying the git clone command, ensure Git is configured using the following commands in the terminal:

git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
Enter fullscreen mode Exit fullscreen mode

These commands configure the global username and email in your Git environment, which will be used when making commits. If Git isn’t configured, you may encounter errors when interacting with repositories.

Let's get to work!

Now that you've cloned your repository, let's create a file, add it to version control, and explore Git's essential commands.

Creating a File and Using Basic Commands

1. Create a file:

  • In Visual Studio, create a new file at the root of your project, for example, a file named index.html. In the terminal, you can do this with the command:
touch index.html
Enter fullscreen mode Exit fullscreen mode
  • Or simply create the file in the Visual Studio editor.

2. Check the Repository Status:

  • To see what has changed in your repository, use the git status command:
Untracked files:
  (use "git add <file>..." to include in what will be committed)
      index.html
Enter fullscreen mode Exit fullscreen mode
  • This means Git has detected the index.html file but is not tracking it yet.

git add Command

3. Add Files to Version Control:

  • To start tracking changes to a file, use the git add command:
git add index.html
Enter fullscreen mode Exit fullscreen mode
  • If you want to add all modified or created files, you can use:
git add .
Enter fullscreen mode Exit fullscreen mode
  • Now the files are ready to be committed.

git commit Command

4. Committing Changes:

  • A commit records changes to the repository. To create a commit, use the command:
git commit -m "Adds index.html file"
Enter fullscreen mode Exit fullscreen mode

Note: The -m flag allows you to add a descriptive message for the commit.

git log Command

5. Check Commit History:

  • To see the history of commits made in the repository, use the git log command:
git log
Enter fullscreen mode Exit fullscreen mode
  • This is the default log option, but Git offers some useful options to simplify and customize the git log output.

Using git log --oneline

  • The oneline mode compresses the display of commits into a single line per commit, showing only the short hash and commit message. Command:
  git log --oneline
Enter fullscreen mode Exit fullscreen mode

Using git log --graph

  • The git log --graph command displays the commits in a graph format, allowing you to visually see the structure of branches and merges. Command:
  git log --graph
Enter fullscreen mode Exit fullscreen mode

Combining git log --graph --oneline --all

  • You can combine several options for a more comprehensive and simple view of the commit history. Combining --graph, --oneline, and --all (which shows commits from all branches - you can know more about branches here) is particularly useful for a clear view of the project's history. Command:
  git log --graph --oneline --all
Enter fullscreen mode Exit fullscreen mode

Output (example):

  * a1b2c3d (HEAD -> master) Adds new feature
  | * 9e8f7g6 (feature) Fixes bug in feature branch
  |/
  * 5h4i3j2 Refactors code for improvements
Enter fullscreen mode Exit fullscreen mode

git checkout Command

This command has multiple uses, but I'll cover just two:

6. Revert Changes:

If you make changes to a file and want to return to the last committed state, use the git checkout command:

git checkout index.html
Enter fullscreen mode Exit fullscreen mode
  • This will undo changes made to index.html that haven't been committed yet.

7. View Project at a Specific Commit:

You can use the git checkout command to "look" at how the project was at a particular commit:

git checkout [commit_hash]
Enter fullscreen mode Exit fullscreen mode

Pushing and Updating the Remote Repository

Now that you've made local changes, you might want to push these changes to the remote repository (GitHub) or pull the latest changes someone else made in the repository.

git pull Command

8. Update Local Repository:

  • Before pushing your changes, it's a good practice to ensure your local copy is up to date with the remote repository. To do this, use:
git pull
Enter fullscreen mode Exit fullscreen mode
  • The git pull command brings changes from the remote repository to your local machine.

git push Command

9. Push Your Changes to the Remote Repository:

  • Now, you can push your local commits to GitHub with the git push command:
git push
Enter fullscreen mode Exit fullscreen mode
  • This will push your changes to the master or main branch on the remote repository.

Did you like the post 🤔? Are you interested in learning more about Git features? Visit: Introduction to branch 😉

💖 💪 🙅 🚩
joel_hanerth
Joel Hanerth

Posted on September 19, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related