Git provides a set of powerful commands for managing source code efficiently. This tutorial explains the most commonly used Git commands, including how to initialize a repository, track changes, work with branches, and push code to a remote repository. By mastering these basic commands, you will be able to manage code versions seamlessly.
Initialize a Git Repository
Before using Git in a project, you need to initialize a repository.
git init
- This command creates a hidden
.git
folder inside the project directory, allowing Git to track changes. - Run this command inside the project folder you want to track with Git.
Check Repository Status
To check the current state of your working directory and staged changes, use:
git status
- It shows the status of tracked and untracked files.
- Displays staged, unstaged, and untracked changes.
Add Files to Staging Area
Before committing changes, you must stage the files using:
git add filename
To add all modified and new files:
git add .
git add filename
stages a specific file for the next commit.git add .
stages all changes in the working directory.
Unstage a File
If you added a file to the staging area but haven't committed it yet:
git reset filename
git reset filename
unstages a file but keeps the changes.
Commit Changes
After staging, commit the changes with a descriptive message:
git commit -m "Your commit message"
- The commit records the current state of the project.
- The
-m
flag allows adding a commit message describing the changes.
Undo the Last Commit
To undo the last commit while keeping changes in the working directory:
git reset --soft HEAD~1
To undo and remove changes permanently:
git reset --soft HEAD~1
moves the commit back but retains the changes.git reset --hard HEAD~1
removes the last commit and all changes permanently.
View Commit History
To see a list of all previous commits, run:
git log
For a compact version:
git log --oneline
git log
shows commit history with details like author, date, and message.git log --oneline
displays a concise history, showing only commit hashes and messages.
Create a Branch
Branches allow you to work on different versions of a project simultaneously. Create a new branch with:
git branch branch-name
- This command creates a new branch without switching to it.
- It helps in managing different features or bug fixes separately.
To see all branches, use:
git branch
Switch Between Branches
To switch to another branch, use:
git checkout branch-name
Alternatively, with Git 2.23+, use:
git switch branch-name
git checkout branch-name
moves to the specified branch.git switch
is a newer and more user-friendly command for switching branches.
Merge Branches
To merge changes from another branch into the current branch:
git merge branch-name
- This command integrates changes from the specified branch into the current branch.
- Run this command from the branch where you want the changes to be merged.
Delete a Branch
After merging a branch, you can delete it using:
git branch -d branch-name
- The
-d
flag deletes the branch if it has been merged. - Use
-D
instead of-d
to force delete an unmerged branch.
Push Changes to a Remote Repository
After committing changes locally, push them to a remote repository (e.g., GitHub):
git push origin branch-name
git push
uploads local changes to the remote repository.origin
is the default name of the remote repository.branch-name
specifies the branch to push.
Pull Changes from a Remote Repository
To fetch and merge the latest changes from the remote repository:
git pull origin branch-name
git pull
downloads changes and merges them into the current branch.- Use this command before making new changes to avoid conflicts.
Clone a Repository
To copy an existing repository to your local machine:
git clone repository-url
Example:
git clone [email protected]:username/repository.git
- This command creates a local copy of a remote repository.
repository-url
is the link to the remote repository.
Conclusion
In this tutorial, you have learned the fundamental Git commands needed for version control. You now know how to initialize a Git repository, track changes, stage and commit files, and manage branches efficiently. Additionally, you have explored how to push and pull changes from a remote repository, clone existing repositories, and undo changes when necessary. With these essential commands, you can confidently manage your source code, collaborate with teams, and maintain an organized workflow in Git.