Jarvis @ JM.me

Using Git Part 1

Oct 5 · 15min امیرمحمد

Git is a distributed version control system that allows multiple developers to track changes, collaborate, and manage project versions efficiently.

What is Git?

Git is a distributed version control system designed to handle everything from small to very large projects with speed and efficiency. It allows multiple developers to work on the same project without conflicting with each other’s changes. Git tracks changes made to files and allows developers to collaborate seamlessly.

With Git, you can:

  • Track the history of your project.
  • Work on different features in parallel using branches.
  • Collaborate with other developers and merge changes efficiently.

How to Initiate Git

To start using Git, you need to install it on your machine:

  1. Install Git:

    • On Linux: sudo apt-get install git
    • On MacOS: brew install git
    • On Windows: Download and install from the Git website.
  2. Configure Git: After installation, configure Git with your name and email (this will be attached to your commits):

    git config --global user.name "Your Name"
    git config --global user.email "[email protected]"
  3. Initialize a Git Repository: Navigate to the project directory where you want to track changes with Git and run the following command:

    git init

    This creates a new Git repository in the project directory.

How to Use Git

Now that Git is initialized, let’s cover some essential commands:

  1. Check the Status of Files: To see the current status of your working directory, run:

    git status
  2. Add Files to the Staging Area: When you create or modify files, Git doesn’t track them until you explicitly tell it to. Add files to the staging area with:

    git add <filename>

    To add all files in the directory:

    git add .
  3. Commit Changes: After adding files to the staging area, you commit the changes with a message:

    git commit -m "Your commit message"
  4. View Commit History: To view the history of commits, run:

    git log

How to Import a New Project into Git

If you have an existing project that isn’t already tracked by Git, you can import it into Git by following these steps:

  1. Navigate to Your Project Directory: Open a terminal and go to your project folder:

    cd /path/to/your/project
  2. Initialize Git: Run the following command to initialize the project as a Git repository:

    git init
  3. Add Files to the Staging Area: Add all project files to the staging area:

    git add .
  4. Commit the Project: Commit the added files with a message:

    git commit -m "Initial commit"
  5. Connect to a Remote Repository (Optional): If you want to push your project to a remote repository (e.g., on GitHub or GitLab), you can do so with:

    git remote add origin <repository-url>
    git push -u origin master

How to Make Changes and Share with Other Developers

Once your project is tracked by Git, you can easily make changes and share them with others. Here’s how:

  1. Create a New Branch (Optional but Recommended): It’s a good practice to work on a new branch when making changes:

    git checkout -b feature-branch
  2. Make Changes and Add Them: Edit files and then add the changes to the staging area:

    git add <filename>
  3. Commit Changes: Commit the changes with a meaningful message:

    git commit -m "Describe the changes you made"
  4. Push Changes to the Remote Repository: Push your changes to the remote repository so other developers can see and pull them:

    git push origin feature-branch
  5. Collaborate with Other Developers: Other developers can clone the repository, make changes, and push their updates. You can pull their changes into your local repository by running:

    git pull

Conclusion

Git is a powerful version control system that helps developers track changes, collaborate on projects, and maintain a clear history of modifications. By following the steps above, you can initialize Git, import projects, make changes, and share your updates with other developers.

> Comment on twitter (X)
 
CC BY-NC-SA 4.0  2009-PRESENT © Nuxsco (AMS) This website rewrite several times from those years up to present