2. Git project

The git init command is first step in using Git in your project. It creates a new Git repository, which is a special directory that stores snapshots of your project’s files and tracks changes over time..

Install Git

To start use git you first need to have it installed.
You find the latest instructions for that here: https://git-scm.com/downloads

How to use it:

  1. Open your terminal or command prompt. This is where you’ll type Git commands.
  2. Navigate to your project directory. Use the cd command to change directories. For example, if your project is in a folder named demo on your desktop, you might type:
  3. Initialize project with: git init
    This creates a hidden folder named .git inside your project directory and in this folder all the Git metadata is stored for your repository.
    A new Git branch named main (or sometimes master, depending on your Git configuration) is created by default. Branches are essentially pointers to specific versions (commits) in your Git history. The main branch initially points to an empty commit.
  4. Add repo from where to pull code from (and later push changes to):
    git remote add origin <address>
    This command establishes a connection between your local Git repository (the one on your computer) and a remote repository (typically hosted on a platform like GitHub, GitLab, or a self-hosted server). By creating this link, you enable actions like:
    • Pushing your local commits (changes) to the remote repository for sharing, collaboration, or backup.
    • Pulling down updates and changes made by others in the remote repository to keep your local copy in sync.
    • git remote: This part of the command tells Git that you’re working with remotes, which are external repositories.
      • add: This keyword instructs Git to add a new remote connection.
      • origin: This is a common naming convention for the remote repository. You can use a different name if you prefer (e.g., upstream, backup), but origin is widely recognized.
      • <url>: This is the placeholder for the actual URL of the remote repository. You’ll need to replace it with the specific URL you obtained from the remote hosting platform or server administrator.
    • After adding the remote:
      You can verify the connection using git remote -v. This should list the remote named origin and its URL.
    • Important notes:
      git remote add origin only creates the connection. You’ll still need to set up authentication (username and password or SSH key) to push or pull from the remote repository. Refer to the documentation of your hosting platform for specific instructions.
      This command is typically used only once in a project to establish the initial remote connection. Subsequent pushes and pulls use other commands like git push and git pull.
  5. To pull all files from main branch execute: git pull origin main
    This will keeps your local project directory up-to-date with the latest changes from a remote repository.
    • git pull: This is the main command that initiates the pull operation. It combines two essential Git actions:
      • git fetch: This retrieves the latest changes from the remote repository (usually named origin) but doesn’t directly integrate them into your local copy.
      • git merge: This integrates (or merges) the fetched changes from the remote branch (main in this case) into your current local branch.
        If the fetched changes from origin/main (the remote main branch) don’t conflict with your current local branch, Git seamlessly merges them into your local branch. This creates a new commit in your local Git history that incorporates the updates from the remote repository.
    • origin: This refers to the name of the remote repository you’ve established a connection with using git remote add origin <url>. It’s a convention, but you could have used a different name during setup.
    • main: This specifies the specific branch in the remote repository that you want to pull changes from. In modern Git workflows, main is the commonly used name for the default branch, but it could be named master in older projects.
    • Conflict Resolution: However, if there are conflicts (i.e., changes made to the same line of code in both the remote and your local branch), Git will halt the merge process and present you with merge conflict markers in the affected files. You’ll then need to manually resolve these conflicts by editing the files and choosing the desired version of the code before attempting another pull.

Example…