Welcome to the Git Essentials tutorial! Here, you'll learn how to start using Git effectively. This guide references multiple platforms and books, providing step-by-step instructions to master Git. Ready to dive in? Let's get started! π
- Why Use Git?
- How Does Git Work?
- Install and Configure Git
- Push Your Code with Git
- Make Changes to Files
- Important Concepts in Git
One of the main reasons to use Git is for version control. It helps you track changes in your code. Imagine having multiple versions of your project stored locally. It can get messy and stressful! π
Using Git simplifies this process. You can change the code and create separate versions as commits, stored locally or remotely. This way, you can access different versions of your project easily. Let's visualize this! π€―
Can Git be used only locally? What if my hard disk is damaged or I lose my computer?
For these situations, you can use a Git Provider to store your projects both locally and on the cloud.
Working with a team? Git makes it easy to sync changes among multiple collaborators. π
Using a Git provider, you can share code with your team effortlessly.
Git is great for collaboration. Multiple people can work on the same project by being added as collaborators. Git providers establish remote connections for easy code sharing. Git can even merge different codes automatically, though sometimes you'll need to do it manually. π€
Open Source means the code is publicly available. You can download, modify, create your version, and publish it. A community often works on open source projects, detecting bugs, fixing them, and improving the code. Git features like branches make open source contributions easy.
Let's explore the technical side of Git. We'll learn how to use Git locally, use Git providers, and understand distributed version control.
When using Git locally, there are three stages:
- Working Directory: Where you make changes.
- Staging Area: Use
git add <filename>
to stage changes. - Local Repository: Use
git commit
to save changes.
With a Git provider, use git push
to upload changes to a remote repository and git pull
to download changes.
In distributed version control, every client has a complete copy of the repository, including all files, snapshots, and history. If something happens to the central repository, each client still has a full copy.
Start by installing Git for your OS from Git Downloads.
Installing Git is straightforward. Follow the prompts and install it.
Configure Git using the gitconfig
file. Important config files include:
- Global:
~/.gitconfig
orC:\Users\[user]\.gitconfig
- Local:
.git/config
Local settings can overwrite global settings.
Type git config --list
in the terminal to view the configuration.
Setting up global configurations:
- Username:
git config --global user.name "Your_name"
- Email:
git config --global user.email "name@example.com"
Learn to create a new repository and work with it. We'll use GitHub as our Git provider.
- Sign up and visit GitHub.
- Name your repository, choose visibility, add a README, and get started.
- Open the GitHub repo.
- Click on the green "Code" dropdown and copy the HTTPS URL.
- In your terminal, type:
git clone <HTTPS URL> cd <repo-name> code .
- Create and save a file.
- Stage the file:
git add <filename> git status git commit -m "first commit" git status
- Push the changes:
git push
- Check the GitHub repo to confirm the changes.
The .git
folder is the heart of your Git repository. It contains all the metadata and object data Git needs to manage the version history.
Structure of the .git
Folder:
- config: Configuration settings.
- HEAD: Points to the current branch reference.
- index: Stores staging area information.
- logs/: Contains logs of reference updates.
- objects/: Stores all objects (blobs, trees, commits).
- refs/: Contains references to commit objects.
- hooks/: Contains scripts triggered by Git actions.
The git status
command provides an overview of your working directory and staging area. It helps track changes and understand your repository's state.
Typical Output:
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: file1.txt
new file: file2.txt
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: file3.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
file4.txt
The git diff
command shows the differences between various states of your repository.
Basic Usage:
- Compare working directory to staging area:
git diff
- Compare staging area to last commit:
git diff --cached
- Compare specific file:
git diff <file>
- Compare two commits:
git diff <commit1> <commit2>
Example Output:
diff --git a/file1.txt b/file1.txt
index d3ff2c6..f7c3f58 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1,3 +1,4 @@
Line 1
Line 2
+New line 3
Line 4
The git log
command displays commit history, including commit hash, author, date, and message.
Basic Usage:
- View commit details:
git show <commit-hash>
- Search for specific commits:
git log --grep='your_commit'
Rename a file with Git using:
git mv current_name new_name
Git doesn't track empty folders by default. To track an empty folder, create a .gitkeep
file inside it.
Undo staged changes:
- Stage a file:
git add example.txt
- Check status:
git status
- Unstage the file:
git restore --staged example.txt
- Verify:
git status
Undo changes to a file:
- Modify a file:
example.txt
- Check status:
git status
- Revert changes:
git restore example.txt
Use git log
to view commit history. To go back to a specific commit:
git checkout <commit-hash>
To return to the latest version:
git checkout main
Revert to a previous commit:
git log --oneline
git revert <commit-id>
Use a .gitignore
file to exclude specific files or folders from being tracked by Git.
Example .gitignore
entry:
example.md
Branches allow you to work on different features or fixes in isolation.
Key Commands:
- Create a branch:
git branch <branch-name>
- Switch to a branch:
git switch <branch-name>
- Create and switch to a new branch:
git switch -c <branch-name>
- List all branches:
git branch
- Merge a branch:
git merge <branch-name>
- Delete a branch:
git branch -d <branch-name>
Good commit messages should:
- Describe changes precisely
- Use present tense
- Be concise (max 50 characters)
- Include additional details if necessary
Merge conflicts occur when changes from different branches conflict. Carefully review and resolve conflicts to ensure a smooth merge.
-
Create a Project and Initialize Git:
-
Create a new project directory.
-
Add an
index.html
file to the project. -
Initialize Git in the project directory and add the file to the local repository:
git init git add index.html git commit -m "Initial commit with index.html"
-
-
Create a New Branch from
master
(or any current branchc1
):-
Create a new branch named
child_branch
:git branch child_branch
-
-
Switch to
child_branch
:-
Checkout the
child_branch
:git checkout child_branch
-
-
Make Changes in
child_branch
and Commit: -
Make Changes in
master
Branch and Commit: -
Merge
child_branch
intomaster
Branch:
When you try to merge child_branch
back into the master
branch, a merge conflict will occur because both branches have made changes to the same file, index.html
.
-
Identify the Conflict:
-
Open the conflicting file (
index.html
) in a text editor. You will see conflict markers like these:<<<<<<< HEAD Changes made in master branch ======= Changes made in child_branch >>>>>>> child_branch
-
-
Resolve the Conflict:
-
Edit the file to resolve the conflict by deciding which changes to keep or by combining the changes:
<!-- Resolved content --> Changes made in master branch Changes made in child_branch
-
-
Mark the Conflict as Resolved:
-
After resolving the conflict, mark the file as resolved:
git add index.html
-
-
Complete the Merge:
-
Commit the merge to complete the process:
git commit -m "Resolved merge conflict between master and child_branch"
-
You have successfully resolved the merge conflict and merged child_branch
into master
!
Congratulations! You've reached the end of the Git Essentials