DevOps

Getting Started with Git: 25 Essential Commands for Beginners

Git is a powerful version control system that every developer should know. Whether you’re collaborating with others or managing your own projects, these 25 Git commands will help you navigate and control your codebase efficiently.

1. git init

Initializes a new Git repository in your project directory.

git init

2. git clone

Clones an existing repository to your local machine.

git clone <repository-url>

3. git status

Displays the status of the working directory and staging area.

git status

4. git add

Stages changes for the next commit.

git add <file-name>
git add .

5. git commit

Commits the staged changes with a message describing the changes.

git commit -m "Your commit message"

6. git push

Uploads your local commits to a remote repository.

git push origin <branch-name>

7. git pull

Fetches and merges changes from a remote repository into your current branch.

git pull origin <branch-name>

8. git branch

Lists, creates, renames, or deletes branches.

git branch
git branch <new-branch-name>

9. git checkout

Switches branches or restores files.

git checkout <branch-name>

10. git merge

Merges changes from one branch into another.

git merge <branch-name>

11. git log

Displays the commit history.

git log

12. git reset

Resets your current branch to a specific commit.

git reset --hard <commit-hash>

13. git fetch

Downloads objects and refs from another repository.

git fetch origin

14. git rebase

Re-applies commits on top of another base tip.

git rebase <branch-name>

15. git remote

Manages remote repository connections.

git remote add origin <repository-url>
git remote -v

16. git diff

Shows changes between commits, branches, or working directory changes.

git diff
git diff <commit1> <commit2>

17. git stash

Temporarily shelves changes in your working directory.

git stash
git stash pop

18. git tag

Creates, lists, deletes, or verifies tags in your repository.

git tag <tag-name>
git tag -l

19. git show

Displays information about a specific commit, tag, or other objects.

git show <commit-hash>

20. git blame

Shows what revision and author last modified each line of a file.

git blame <file-name>

21. git rm

Removes files from the working directory and the index.

git rm <file-name>

22. git mv

Moves or renames a file, directory, or symlink.

git mv <old-file-name> <new-file-name>

23. git bisect

Uses binary search to find the commit that introduced a bug.

git bisect start
git bisect bad
git bisect good <commit-hash>

24. git reflog

Shows a log of all the references in the repository, including branch heads and other refs.

git reflog

25. git cherry-pick

Applies the changes introduced by some existing commits.

git cherry-pick <commit-hash>

Conclusion

These 25 Git commands cover the essentials for managing a Git repository. By mastering these, you’ll have a strong foundation for working with Git in any development environment. Whether you’re just starting out or looking to deepen your Git knowledge, these commands will serve you well.

If you have any questions or want to share your experiences with Git, feel free to comment below!

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *