Installing Git
To use Git, you must first install it on your system. Below are the installation methods for major operating systems.
Installing Git on Linux
Most Linux distributions allow you to install Git via a package manager. In many cases, it is already pre-installed.
Installing Git on Debian/Ubuntu
sudo apt update
sudo apt install git
Use the APT package manager to install Git.
Installing Git on Fedora/CentOS/RHEL
sudo yum install git # CentOS/RHEL 7 or earlier
sudo dnf install git # Fedora/CentOS/RHEL 8 or later
Use the YUM or DNF package manager to install Git.
Git Configuration (First Time Only)
After installing Git, you must configure the username and email address to be used for commits. This information is recorded with each commit.
Set User Name
git config --global user.name "Your Name"
Set the username that will appear in Git commits.
Set Email Address
git config --global user.email "your_email@example.com"
Set the email address that will appear in Git commits.
Check Configuration
git config --list
Check the current Git configuration.
Git Basic Concepts
There are several core concepts you need to know to use Git effectively. These concepts help you understand how Git works and optimize your workflow.
Main Concepts of Git
- Repository: A space where all project files, change history, and metadata are stored. It is divided into local and remote repositories.
- Commit: A unit that records a snapshot of code changes in the repository. Each commit has a unique ID (hash) and includes the differences from the previous commit.
- Branch: A pointer that creates an independent development flow. It allows you to work on new features or bug fixes without affecting the main codebase.
- Merge: The process of combining changes from different branches into one.
- Working Directory: The workspace where the actual files exist. This is where you modify files.
- Staging Area (Index): A temporary storage space for changes in the working directory that you want to include in the next commit. You add files to this area with the `git add` command.
Key Git Commands
These are the core commands used to initialize a Git project, track changes, and collaborate. Combine these commands to perform efficient version control.
1. Initialize and Clone Repository
2. Track Changes and Commit
3. Branch Management and Merging
4. Interacting with Remote Repositories
Generated command:
Try combining the commands.
Description:
`git` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Through practical examples of Git commands, you can understand the core flow of version control and practice it yourself.
Create a new Git repository and make the initial commit
mkdir my_new_project
cd my_new_project
git init
echo "# My New Project" > README.md
git add README.md
git commit -m "Initial project setup"
This is the process of initializing a Git repository and committing the first changes when starting a new project.
Clone a remote repository and start working
git clone https://github.com/octocat/Spoon-Knife.git
cd Spoon-Knife
git status
Clones an existing GitHub repository to your local machine and checks the status of the cloned repository.
Create and switch to a branch for new feature development
git checkout -b feature/add-login
Creates a new branch named `feature/add-login` from the `main` branch and immediately switches to it to start working.
Add, commit, and push changes to remote
echo "console.log('Login feature added');" >> src/login.js
git add src/login.js
git commit -m "Add login feature base structure"
git push origin feature/add-login
Modifies a file, adds it to the staging area, commits with a message, and then pushes the changes to the remote repository.
Fetch and merge changes from another branch
git checkout main
git pull origin main # Update to the latest main branch state
git merge feature/add-login
git branch -d feature/add-login
git push origin main
Switches to the `main` branch, merges the changes from the `feature/add-login` branch into the `main` branch, and then deletes the feature branch.