Git is a powerful version control system that is widely used by developers around the world. It allows you to keep track of your code changes, revert back to previous versions, and collaborate with others.
Here are the top 25 git commands list with examples, along with explanations of each one:
BASICS GIT COMMANDS:
1. how to git init a folder?
git init
– Initializes a new Git repository.
git init
2. how to git clone?
git clone
– Makes a copy of an existing Git repository.
git clone [repository-url]
git clone https://github.com/debauchee/barrier.git
3. how to git add only modified files?
git add
– Adds a file to the staging area.
git add [file-name]
4. how to use git commit?
git commit
– Saves changes to the local repository.
git commit -m "[commit-message]"
git commit -m "Project changes"
5. how to git push origin master?
git push
– Sends commits to a remote repository.
git push [remote-name] [branch-name]
git push origin master
6. how to git pull origin master?
git pull
– Downloads commits from a remote repository.
git pull [remote-name] [branch-name]
git pull origin master
7. how to use git branch command?
git branch
– Lists, creates or deletes branches.
#list of branches
git branch
create "sample-branch"
git branch sample-branch
delete "sample-branch"
git branch -d sample-branch
8. how to use git checkout master?
git checkout
– Switches between branches or restores files.
# To switch to an existing branch
git checkout [branch-name]
#To switch to a new branch
git checkout -b [branch-name]
# restore a file from the most recent commit
git checkout HEAD -- [file-name]
9. how to merge two branches in git?
git merge
– Merges branches together.
git merge [source-branch] [target-branch]
10. What is git status?
git status
– Shows the status of files in the working directory and staging area.
git status
ADVANCE GIT COMMANDS:
11. what is git show command?
git show
– Shows information about a specific commit.
git show [commit-id]
12. How to git log specific branch?
git log
– Shows the commit history for the current branch.
git log
13. How to git diff between two branches?
git diff
– Shows differences between commits, branches, or files.
#o view the differences between two commits
git diff [commit-id-1] [commit-id-2]
#To view the differences between two branchs
git diff [branch-name-1] [branch-name-2]
#To view the differences between two files
git diff [file-name-1] [file-name-2]
14. what does git stash do?
git stash
– Temporarily saves changes that are not ready to be committed.
git stash
15. what is git stash apply?
git stash apply
– Restores changes from the most recent stash.
git stash apply
16.
16. what is git stash drop?
git stash drop
– Discards the most recent stash.
git stash drop
17. How to see git stash list?
git stash list
– Shows a list of all stashes.
git stash list
18. How to clear git stash list
git stash clear
– Discards all stashes.
git stash clear
19. how to git tag a commit?
git tag
– Tags a specific commit with a label.
git tag [tag-name] [commit-id]
20.
20. What is git fetch?
git fetch
– Downloads commits and tags from a remote repository, but does not merge them.
git fetch [remote-name]
21.
21. All about git reset command
git reset
– Discards commits, moves the branch pointer, or restores files.
#To discard commits
git reset [commit-id]
#To move the branch pointer
git reset [branch-name]
#To restore files
git reset [file-name]
22. What is git rebase?
git rebase
– Reapplies commits on top of another base commit.
git rebase [base-branch] [branch-name]
23. What does cherry-picking a commit with Git mean?
git cherry-pick
– Applies the changes introduced by a commit.
git cherry-pick [commit-id]
24.
24. Git bisect explained
git bisect
– Finds the commit that introduced a bug.
git bisect start
To mark a commit as good or bad, use the git bisect good
or git bisect bad
command, respectively.
To end the bisect process, use the following command:
git bisect reset
25. What does ‘git blame’ do?
git blame
– Shows who last modified each line of a file.
git blame [file-name]
The Short Note:
Related blog: How to reinstall a package using apt-get? – Debian/Ubuntu
I hope this list of famous git commands and examples has been helpful! Let me know if you have any questions.