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:
- Open your terminal or command prompt. This is where you’ll type Git commands.
- Navigate to your project directory. Use the
cdcommand to change directories. For example, if your project is in a folder nameddemoon your desktop, you might type: - Initialize project with: git init
This creates a hidden folder named.gitinside your project directory and in this folder all the Git metadata is stored for your repository.
A new Git branch namedmain(or sometimesmaster, depending on your Git configuration) is created by default. Branches are essentially pointers to specific versions (commits) in your Git history. Themainbranch initially points to an empty commit. - 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), butoriginis 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 usinggit remote -v. This should list the remote namedoriginand its URL. - Important notes:
git remote add originonly 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 likegit pushandgit pull.
- 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 namedorigin) but doesn’t directly integrate them into your local copy.git merge: This integrates (or merges) the fetched changes from the remote branch (mainin this case) into your current local branch.
If the fetched changes fromorigin/main(the remotemainbranch) 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 usinggit 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,mainis the commonly used name for the default branch, but it could be namedmasterin 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…
$ mkdir demo
$ cd demo
$ git init
Initialized empty Git repository in /home/jonas/demo/.git/
$ git remote add origin https://gitserver/jonas/git-demo.git
-rw-r--r-- 1 jonas 7 Apr 21 11:33 file_1.txt
$ git pull origin main
remote: Enumerating objects: 18, done.
remote: Counting objects: 100% (15/15), done.
remote: Compressing objects: 100% (13/13), done.
remote: Total 18 (delta 1), reused 0 (delta 0), pack-reused 3
Unpacking objects: 100% (18/18), 1.53 KiB | 34.00 KiB/s, done.
From https://gitserver/jonas/git-demo
* branch main -> FETCH_HEAD
* [new branch] main -> origin/main
$ ls -oA
drwxr-xr-x 1 jonas 512 Apr 21 11:33 .git
-rw-r--r-- 1 jonas 65 Apr 21 11:33 README.md