Simple Git Commands Every Developer Must Know
11 essential git commands to know for any software project
Whether you're just starting your coding journey or a seasoned pro, understanding git and its core commands is essential for efficient collaboration and version management. In this article, we'll walk you through the fundamental Git commands that every developer must know, providing clear explanations and examples.
What is Git?
Git is a distributed version control system that allows developers to track changes to their codebase, collaborate with others, and manage project versions effectively. It offers a decentralized workflow, enabling multiple developers to work on the same codebase simultaneously without conflicts.
Essential Git Commands:
#1. git init
Use git init
to initialize a new Git repository in your project directory. This command creates a hidden .git
folder, which stores your project's version control information.
Example:
bash
$ mkdir my_project # Creates a new project directory
$ cd my_project
$ git init # Initializes git to track changes
#2. git add
The git add
command adds changes in your working directory to the staging area, preparing them to be committed.
Example:
bash
- stage a single file "file1.py"
$ git add file1.py # stages "file1.py"
- stage all the files in the working dir
$ git add .
#3. git status
To view the status of your working directory and see which files are staged or modified, use git status
.
Command:
bash
$ git status
#4. git commit
After staging your changes, use git commit to permanently store them in the repository with a descriptive message.
Example:
bash
$ git commit -m "Add feature XYZ"
#5. git log
The git log
command displays a chronological list of commits in the repository.
Example:
bash
$ git log
#6. git branch
To create, list, or delete branches in your repository, use git branch
.
Example:
bash
- create a new branch
$ git branch dev # Creates a branch called "dev"
- list all branches
$ git branch
- delete a branch
$ git branch -D quick-audit # deletes 'quick-audit' branch
#7. git checkout
To switch between branches or restore files to a previous state, use git checkout
.
Example:
bash
- switch to an existing branch "dev"
$ git checkout dev # Switches to dev branch
- create and switch to a new branch
$ git checkout -b feature-1 # Create and switch to "feature-1" branch
#8. git merge
The git merge
command combines changes from different branches into your current branch.
Example:
bash
$ git merge feature-1 # merges the feature-1 branch to the current branch
#9. git remote
This command creates connections with remote repositories on Github, GitLab or BitBucker.
Example:
bash
- list all remote connection
$ git remote
- create a connection to a remote repo with a reference
$ git remote add origin <link-to-remote-repo> # Creates new connection to remote repo with the reference "origin"
- disconnect from a remote repo
$ git remote remove origin # Removes the remote reference to origin
#10. git push
After committing your changes, use git push
to upload them to a remote repository, enabling collaboration with others.
Example:
bash
- sync changes from a local branch to an existing branch in a remote repository
$ git push origin dev
- sync changes from a local branch to a non-existing branch in a remote repository
$ git push origin -u dev # Creates and updates a branch "dev" in the remote repo
- sync changes from a local branch to a matching branch in the remote repository
$ git push
#11. git pull
Use git pull
to fetch and merge changes from a remote repository to your local branch.
Example:
bash
- sync changes from a remote branch to a local branch
$ git pull origin main # Pulls changes to the remote branch "main"
- sync all changes from the remote branch
$ git pull
Conclusion
Mastering these essential Git commands lays a strong foundation for effective version control and collaboration in your development projects. Whether you're a beginner or an experienced developer, understanding Git is a valuable skill that will streamline your workflow and enhance productivity.
Ready to take your coding skills to the next level? Join Dome Academy's vibrant community of developers and learners today! Sign up for our newsletter to stay updated on the latest tech trends, courses, and exclusive offers. Let's embark on this coding journey together!