Understanding basics of git and github

What is git?

It is a Version control system (it is a tool that helps to track changesin code) which is very fast and scalable. Anyone can use it and contribute to it is free and open source. it is even used across MNCs and other big companies across developers to share and manage code with their teams. which helps to manage large code bases and to keep a track of changes and progress by the whole team . which in terms helps in collaborating with teams.

learning git

Git repository :- A Git repository is a storage space where your project files and the entire history of changes made to those files are stored. It allows you to track changes, collaborate with others, and manage different versions of your project. it is created when we initialise a git in a folder .

Setup git

First step is to set up an account with git on your terminal for that we install git by using a command

sudo apt install git

This command will install git in your system and you need to have an account on github, gitlabs etc. After that we need to configure our machine to work with your online account this is done by using the following command.

git config --global user.name "username"
git config --global user.email "useremail@example.com"

After using this command you can use your terminal to start using git in your terminal.

A Simple Workflow with Git and GitHub

Let’s walk through a typical workflow for developers using Git and GitHub:

  1. Create a Repository: On GitHub, you create a new repository for your project. This acts as the central hub for your code.

  2. Clone the Repository: You clone the repository from GitHub to your local machine. This creates a local copy of the code that you can modify.

  3. Make Changes Locally: You work on your code locally, creating new features, fixing bugs, or improving documentation.

  4. Commit Changes: Once you’ve made your changes, you “commit” them with Git. This records your changes and keeps a history of your work.

  5. Push to GitHub: After committing locally, you push your changes to the remote GitHub repository. This syncs your local changes with the cloud, making them visible to your team or other collaborators.

  6. Collaborate: Team members can now pull your changes, comment on them, or even propose their own changes via pull requests.

  7. Merge: Once everyone is happy with the changes, they’re merged into the main branch, and the project continues to evolve.

Here are some basic Git commands with a one-line explanation and syntax:

1. git init

  • Explanation: Initializes a new Git repository in the current directory.

  • Syntax:

      git init
    

2. git clone

  • Explanation: Creates a copy of a remote repository on your local machine.

  • Syntax:

      git clone <repository_url>
    

3. git status

  • Explanation: Displays the current state of your working directory and staging area.

  • Syntax:

      git status
    

4. git add

  • Explanation: Stages changes in your working directory to be committed.

  • Syntax:

      git add <file_name>   # For specific files
      git add .             # For all files
    

5. git commit

  • Explanation: Records the staged changes with a message describing them.

  • Syntax:

      git commit -m "Your commit message"
    

6. git push

  • Explanation: Uploads local commits to a remote repository.

  • Syntax:

      git push <remote> <branch>
      git push origin main  # generally used like this
    

7. git pull

  • Explanation: Fetches and integrates changes from a remote repository into your local branch.

  • Syntax:

      git pull <remote> <branch>
    

8. git branch

  • Explanation: Lists, creates, or deletes branches.

  • Syntax:

      git branch            # List branches
      git branch <branch_name>  # Create a new branch
      git branch -d <branch_name>  # Delete a branch
    

9. git checkout

  • Explanation: Switches between branches or restores files in your working directory.

  • Syntax:

      git checkout <branch_name>    # Switch to a branch
      git checkout -- <file_name>   # Discard changes in a file
    

10. git merge

  • Explanation: Combines changes from one branch into another.

  • Syntax:

      git merge <branch_name>
    

11. git log

  • Explanation: Shows the commit history for the current branch.

  • Syntax:

      git log
    

12. git remote

  • Explanation: Manages remote repositories (e.g., adding, removing, or listing).

  • Syntax:

      git remote add <name> <url>   # Add a remote
      git remote -v                # List remotes
    

13. git diff

  • Explanation: Shows the differences between your working directory and the staged changes.

  • Syntax:

      git diff
    

14. git reset

  • Explanation: Unstages files or resets the repository to a previous commit.

  • Syntax:

      git reset <file_name>      # Unstage a file
      git reset --hard <commit>  # Reset to a specific commit (be cautious)
    

These are the most common Git commands and their basic syntax.

Conclusion

git it is like a trusty notebook for developers, helping us keep track of every change we make to our code. It makes collaboration easier, ensuring that no one accidentally overwrites anyone else's work. GitHub, on the other hand, takes Git and adds a cloud-based platform for sharing and managing our projects. It’s perfect for working with teams, contributing to open-source projects, or just keeping your code organized. Once you get the hang of the basic Git commands, you’ll find that working with code becomes a lot smoother and more fun, whether you’re flying solo or collaborating with others.

0
Subscribe to my newsletter

Read articles from Chaitanya Aggarwal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Chaitanya Aggarwal
Chaitanya Aggarwal