Git Commands - An Essential Guide for Developers
Git is an indispensable tool for managing source code versions in development projects. This post aims to introduce a wide range of Git commands, from basics to advanced usage, providing usage examples and real-life application scenarios to help you utilize Git more effectively.
Installing and Configuring Git
The first step is installing Git, which is available for all major operating systems including Windows, macOS, and Linux.
You can get download and install Git through the link below.
After installation, it’s essential to configure your user name and email address before you start using Git, as this information is used in every commit.
1
2
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
Basic Git Commands
Initializing a Repository: git init
Used at the beginning of a new project or when introducing Git to an existing project. It initializes a new Git repository in the current directory.
1
git init
Staging Changes: git add
Adds changes in files or directories to the staging area, selecting changes for the next commit.
1
2
3
git add <file>
git add . # All
Committing Changes: git commit
Records the staged changes to the repository. Each commit has a unique ID, representing a snapshot of changes at a specific point in time.
1
git commit -m "Commit message"
Checking Repository Status: git status
Shows the current state of the repository, including changes files, stages files, and uncommitted changes.
1
git status
Viewing Commit History: git log
Displays the commit history of the repository, showing detailed information about each commit, including the author and date.
1
git log
Undoing Changes
Changing the Working Directory: git checkout
Changes the state of the working directory to a specific branch or commit. This can be used to revert to a previous state or switch to another branch.
1
git checkout <branch-name or commit-hash>
Branching and Merging
Listing and Creating Branches: git branch
Shows the list of branches in the repository or creates a new branch.
1
2
git branch # List branches
git branch <new-branch-name> # Create a new branch
Merging Branches: git merge
Merges changes from another branch into the current branch. This process may result in conflict that need to be resolved manually.
1
git merge <branch-name>
Working with Remote Repositories
Adding a Remote Repository: git remote add
Adds a new remote repository. This allows for integration with remote hosting services like GitHub.
1
git remote add <shortname> <url>
Fetching Changes from a Remote Repository: git fetch
Fetches the latest changes from a remote repository to local, without altering the current branch’s work.
1
get fetch <remote-name>
Uploading Changes to a Remote Repository: git push
To upload your local changes to a remote repository, use the git push
command. This command uploads the committed changes from the current branch to the remote repository, facilitating sharing and collaboration with team members.
This command is essential for pushing local changes to a remote server like GitHub, allowing others to see the updates you’ve made. It’s a fundamental aspect of collaborative work in development projects, ensuring that everyone’s work is synchronized and up-to-date.
1
git push <remote-name> <branch-name>
Advanced Git Commands
Stashing Changes: git stash
Temporarily stores current changes, allowing you to revert the working directory to a clean state.
1
git stash
These Git commands are essential tools that developers use daily. Understanding and utilizing them is crucial for effective source code managemnet. Apply each command in appropriate situations to leverage Git’s powerful features to the fullest.