git init:
This command initializes a new Git repository in your current directory. Think of it as creating a box to store all your project’s versions.
Example: Let’s say you have a folder named “my_project” where you’re working on a website. Open your terminal or command prompt and navigate to that directory using the “cd” command. Then, type:
git init
This will create a hidden folder called “.git” inside “my_project” to store Git’s information.
git add:
This command tells Git to start tracking specific files or changes you’ve made. It prepares them for the next step, which is creating a snapshot.
Example: After making some edits to a file named “kmca.html” in your project, you want to tell Git to track those changes. Use the following command:
git add kmca.html
You can also use git add . to add all modified files in the current directory.
git commit:
This command creates a snapshot of the currently tracked files in your Git repository. It’s like taking a picture of your project at a specific point in time, along with a message describing the changes.
Example: After adding the modified “index.html” file, you can commit it with a message using:
git commit -m "Updated the website layout for Kyurious Minds"
The “-m” flag tells Git to include the message “Updated the website layout” with your commit.
git status:
This command shows you the current status of your Git repository. It tells you which files are tracked, untracked, or staged (added but not yet committed).
Example: At any point, you can type:
git status
This will list any changes you’ve made and whether they are ready to be committed.
git log:
This command shows you the history of commits in your Git repository. It displays each commit’s message, author, and date.
Example: Use:
git log
to see a list of your commits so far. This helps you track the progress of your project.
git branch
Git branches are used to work on different parts of a project simultaneously without affecting the main codebase. To create a new branch named “feature-branch”, you can use the following command:
git branch feature-branch
git checkout
The git checkout command is used to switch between branches. To switch to the “feature-branch” we just created, use the following command:
git checkout feature-branch
git merge
Git merge is used to integrate changes from one branch into another. Let’s merge changes from “feature-branch” into the main branch:
git checkout main
git merge feature-branch
git remote
Git remote allows you to interact with remote repositories. To add a remote repository named “origin” pointing to a GitHub repository, use the following command:
git remote add origin <remote_repository_url>
git clone
Git clone is used to create a local copy of a remote repository. To clone a repository from GitHub, use the following command:
git clone <repository_url>
git pull
Git pull is used to fetch and merge changes from a remote repository into the current branch. To pull changes from the remote repository into the current branch, use the following command:
git pull origin main
git push
Git push is used to push local changes to a remote repository. To push changes from the current branch to the remote repository, use the following command:
git push origin main
Putting It All Together
Let’s illustrate the workflow with an example:
- Create a new branch named “feature-branch”:
git branch feature-branch
- Switch to the “feature-branch”:
git checkout feature-branch
- Make some changes to the code and commit them:
git add .
git commit -m "Added new feature"
- Switch back to the main branch and merge changes from “feature-branch”:
git checkout main
git merge feature-branch
- Push changes to the remote repository:
git push origin main