DEV Community

Cover image for Essential git commands every developer should know
Christopher Glikpo  ⭐
Christopher Glikpo ⭐

Posted on

Essential git commands every developer should know

Git is a powerful and widely-used version control system that helps developers manage their code, collaborate with others, and track changes over time. As a software engineer, mastering Git is essential for streamlining your workflow and ensuring that your projects run smoothly. In this blog post, we'll cover some of the most essential Git commands that every developer should know.

1. git init

The git init command initializes a new Git repository in your current directory. This command creates a hidden .git folder, which contains all the necessary metadata for version control.

Usage:

git init
Enter fullscreen mode Exit fullscreen mode

2. git clone

The git clone command creates a copy of a remote repository on your local machine. This is useful when you want to work on an existing project or contribute to an open-source project.

Usage:

git clone <repository_url>
Enter fullscreen mode Exit fullscreen mode

3. git status

The git status command shows the current state of your working directory, including any changes you've made, new files, and files that have been deleted.

Usage:

git status
Enter fullscreen mode Exit fullscreen mode

4. git add

The git add command stages changes in your working directory for the next commit. You can add individual files or entire directories.

Usage:

git add <file_or_directory>
Enter fullscreen mode Exit fullscreen mode

5. git commit

The git commit command creates a new commit with your staged changes, along with a descriptive message. This command is essential for tracking changes in your codebase over time.

Usage:

git commit -m "Your commit message"
Enter fullscreen mode Exit fullscreen mode

6. git log

The git log command displays a log of all the commits in your repository, allowing you to view the history of your project.

Usage:

git log
Enter fullscreen mode Exit fullscreen mode

7. git diff

The git diff command shows the differences between your working directory and the latest commit. This is useful for reviewing changes before committing them.

Usage:

git diff
Enter fullscreen mode Exit fullscreen mode

8. git pull

The git pull command fetches changes from a remote repository and merges them into your local branch. This is essential for keeping your local repository up-to-date with the latest changes from your team.

Usage:

git pull <remote> <branch>
Enter fullscreen mode Exit fullscreen mode

9. git push

The git push command sends your local commits to a remote repository, allowing you to share your changes with others.

Usage:

git push <remote> <branch>
Enter fullscreen mode Exit fullscreen mode

10. git branch

The git branch command lists all the branches in your repository and shows the current active branch.

Usage:

git branch
Enter fullscreen mode Exit fullscreen mode

11. git checkout

The git checkout command switches between branches or commits in your repository. This is useful for working on different features or bug fixes simultaneously.

Usage:

git checkout <branch_or_commit>
Enter fullscreen mode Exit fullscreen mode

12. git merge

The git merge command merges changes from one branch into another. This is essential for integrating work from multiple developers or merging feature branches into the main branch.

Usage:

git merge <source_branch>
Enter fullscreen mode Exit fullscreen mode

13. git fetch

The git fetch command retrieves updates from a remote repository but does not merge them. This allows you to review changes before merging them into your local branch.

Usage:

git fetch <remote>
Enter fullscreen mode Exit fullscreen mode

14. git remote

The git remote command lists all remote repositories connected to your local repository. This is useful for managing multiple remote repositories or checking the status of your connections.

Usage:

git remote
Enter fullscreen mode Exit fullscreen mode

15. git stash

The git stash command temporarily saves changes in your working directory that you do not want to commit yet. This is helpful when you need to switch branches or work on a different task without committing your current changes.

Usage:

git stash
Enter fullscreen mode Exit fullscreen mode

16. git stash apply

The git stash apply command applies the changes you previously stashed using git stash. This allows you to retrieve your saved changes and continue working on them.

Usage:

git stash apply
Enter fullscreen mode Exit fullscreen mode

17. git rebase

The git rebase command re-applies commits from one branch onto another, effectively rewriting the commit history. This is useful for maintaining a linear history and simplifying complex branching structures.

Usage:

git rebase <base_branch>
Enter fullscreen mode Exit fullscreen mode

18. git cherry-pick

The git cherry-pick command applies specific commits from one branch to another. This is helpful when you want to include only certain changes from a branch without merging the entire branch.

Usage:

git cherry-pick <commit_hash>
Enter fullscreen mode Exit fullscreen mode

19. git tag

The git tag command creates a reference to a specific commit, usually to mark a release or milestone. Tags are useful for tracking important points in your project's history.

Usage:

git tag <tag_name> <commit_hash>
Enter fullscreen mode Exit fullscreen mode

20. git config

The git config command allows you to configure Git settings, such as your username, email, and default text editor. Proper configuration is essential for ensuring that your commits are correctly attributed and that your Git environment is tailored to your preferences.

Usage:

git config --global <setting> <value>
Enter fullscreen mode Exit fullscreen mode

21. git reset

The git reset command allows you to undo changes in your working directory or reset your commit history. This can be useful for reverting to a previous state or removing unwanted commits.

Usage:

git reset <commit_hash>
Enter fullscreen mode Exit fullscreen mode

22. git rm

The git rm command removes files from your working directory and stages the removal for the next commit. This is helpful when you need to delete files from your repository.

Usage:

git rm <file>
Enter fullscreen mode Exit fullscreen mode

23. git bisect

The git bisect command is a powerful tool for finding the commit that introduced a bug or issue. By performing a binary search through your commit history, git bisect can quickly identify the problematic commit.

Usage:

git bisect start
git bisect bad <commit_hash>
git bisect good <commit_hash>
Enter fullscreen mode Exit fullscreen mode

24. git show

The git show command displays information about a specific commit, such as the commit message, author, and changes made. This is useful for reviewing the details of a particular commit.

Usage:

git show <commit_hash>
Enter fullscreen mode Exit fullscreen mode

25. git blame

The git blame command shows the last modification for each line in a file, along with the commit hash, author, and date. This is helpful for identifying who made specific changes and when they were made.

Usage:

git blame <file>
Enter fullscreen mode Exit fullscreen mode

26. git reflog

The git reflog command displays a log of all the actions performed in your local repository, including commits, checkouts, and resets. This can be useful for recovering lost commits or understanding the history of your local actions.

Usage:

git reflog
Enter fullscreen mode Exit fullscreen mode

27. git clean

The git clean command removes untracked files from your working directory. This is helpful for cleaning up your workspace and ensuring that only relevant files are included in your repository.

Usage:

git clean -n (to preview changes)
git clean -f (to execute the cleanup)
Enter fullscreen mode Exit fullscreen mode

28. git submodule

The git submodule command allows you to manage external repositories within your main repository. This is useful for including third-party libraries or separating your project into smaller, more manageable components.

Usage:

git submodule add <repository_url> <path>
Enter fullscreen mode Exit fullscreen mode

29. git grep

The git grep command searches your repository for a specified pattern. This is helpful for finding specific code snippets, comments, or keywords within your project.

Usage:

git grep <pattern>
Enter fullscreen mode Exit fullscreen mode

30. git archive

The git archive command creates a compressed archive of your repository at a specific commit or branch. This is useful for creating backups or distributing your project as a single file.

Usage:

git archive --format=<format> -o <output_file> <commit_or_branch>
Enter fullscreen mode Exit fullscreen mode

Conclusion:

These essential Git commands will help you manage your code, collaborate with others, and track changes in your projects. As a software engineer, mastering these commands is crucial for streamlining your workflow and ensuring the success of your projects. Remember to practice using these commands regularly and explore additional Git features to further enhance your version control skills.

Top comments (1)

Collapse
 
sohrab09 profile image
Mohammad Sohrab Hossain

Good article.