13 Essential Git Commands for Developers
Git is a crucial skill for developers. Learn these 13 Git commands because you'll be using them 99% of the time. This blog covers `git init`, `git clone`, `git add`, and more.
Pratham
DevRel @HyperspaceAI • Prev @Rapid_API • Partners @iteration_x @10Web_io @StockImgAI @JamDotDev @CohesiveAI @refine_dev @chatlingai @Inboxproio @Docus_ai
-
Git is a crucial skill for developers.
— Pratham (@Prathkum) June 8, 2023
Learn these 13 Git commands because you'll be using them 99% of the time: -
1. `git init`
— Pratham (@Prathkum) June 8, 2023
Initialize a new Git repository.
This creates a new subdirectory named ".git" in the current directory, where Git stores all the metadata for the repository. -
2. `git clone`
— Pratham (@Prathkum) June 8, 2023
Clone an existing repository.
This creates a local copy of the repository, including all of its history and branches.
`git clone <repo-link>` -
3. `git add`
— Pratham (@Prathkum) June 8, 2023
Stage changes for the next commit.
This adds the specified file(s) to the staging area, where they will be included in the next commit.
`git add file1.txt file2.txt` -
4. `git commit`
— Pratham (@Prathkum) June 8, 2023
Create a new commit.
This records the staged changes and any additional changes made since the last commit, along with a commit message describing the changes.
`git commit -m "Add new feature"` -
5. `git push`
— Pratham (@Prathkum) June 8, 2023
Push commits to a remote repository.
This sends the local commits to the specified remote repository, updating the branch on the remote with the new commits.
`git push origin main` -
6. `git pull`
— Pratham (@Prathkum) June 8, 2023
Fetch and merge changes from a remote repository.
This retrieves the latest commits from the specified remote repository and merges them into the current branch.
`git pull origin main` -
7. `git branch`
— Pratham (@Prathkum) June 8, 2023
List, create, or delete branches.
This command can be used to list the available branches in a repository, create a new branch, or delete an existing branch.
`git branch new-branch` -
8. `git checkout`
— Pratham (@Prathkum) June 8, 2023
Switch to a different branch.
This command allows you to switch to a different branch in the repository and make it the current working branch.
`git checkout main` -
9. `git merge`
— Pratham (@Prathkum) June 8, 2023
Merge one branch into another.
This command combines the changes from one branch into another branch, creating a new commit that reflects the merged changes.
`git merge new-branch` -
10. `git status`
— Pratham (@Prathkum) June 8, 2023
Show the status of the repository.
This command displays the current branch, any staged or unstaged changes, and any untracked files.
`git status` -
11. `git rebase`
— Pratham (@Prathkum) June 8, 2023
Incorporate changes from one branch into another branch.
Let's say you have made some changes on an "XYZ" branch that you want to incorporate into the "main" branch. You can use git rebase to reapply your changes on top of the main branch. pic.twitter.com/uzjl6dpHHl -
12. `git stash`
— Pratham (@Prathkum) June 8, 2023
Temporarily save changes that you are not yet ready to commit.
If your changes are not ready to commit but you want to work on a different branch, you can temporarily save your changes for later use and switch branches without losing your progress. pic.twitter.com/QynRcvP70k -
13. `git revert`
— Pratham (@Prathkum) June 8, 2023
Let's say you have made a mistake in a previous commit and you need to undo it. You can use git revert to create a new commit that undoes the changes introduced by the previous commit.
`git revert <commit1>..<commit2>` -
This is the end of this thread.
— Pratham (@Prathkum) June 8, 2023
What else would you like to include?