DEV Community

Cover image for Top 15 Git Commands That Every Developer Should Know
Hardik Mervana
Hardik Mervana

Posted on

Top 15 Git Commands That Every Developer Should Know

Version control systems like Git allow developers to collaborate on code and track changes over time. Git is a distributed version control system, meaning each developer has a full copy of the repository with complete history and tracking abilities.

Git's widespread adoption and utility make it an indispensable tool for any developer working on large projects or in a team. Learning Git starts with understanding the basic commands to track code, synchronize changes between repositories, and collaborate with others.

Here are the top 15 Git commands that every developer should know:

1. git init

The git init command initializes a new Git repository in the current directory. It creates all the necessary files and folders for Git to start tracking changes made to files in that folder.

git init
Enter fullscreen mode Exit fullscreen mode

Running this command creates a new .git subfolder that contains the repository data. This command should be run once per repository to set it up.

2. git status

The git status command displays the state of the working directory and staging area. It shows which files are untracked, modified, or staged for the next commit.

git status
Enter fullscreen mode Exit fullscreen mode

Running git status lets you see which changes have been made since the last commit. This command is useful for getting a summary of the project state before making a commit.

3. git add

The git add command stages files to be committed. Git tracks changes to files, but will not commit the changes until they are staged.

git add <file>
Enter fullscreen mode Exit fullscreen mode

Adding files with git add marks them to be included in the next commit. This command needs to be run each time you want to commit new changes.

4. git commit

The git commit command commits the staged snapshot to the project history. Commits should be made frequently with descriptive messages explaining the changes.

git commit -m "Commit message in present tense"
Enter fullscreen mode Exit fullscreen mode

The commit message flags the purpose of that commit. Well-written messages help document the change history of a file or project.

5. git log

The git log command displays the commit history log for the repository or a file. It lists details like commit hash, author, date, and commit messages.

git log
git log -p <file>
Enter fullscreen mode Exit fullscreen mode

Reviewing git log lets you browse previous commits and understand how the repository or file evolved. The -p flag shows the full diff of each commit's changes.

6. git diff

The git diff command shows unstaged changes between the working directory and staging area. This displays changes that have been made but not yet staged for commit.

git diff
Enter fullscreen mode Exit fullscreen mode

git diff without arguments displays all uncommitted changes. You can also compare differences between commits and branch states.

7. git checkout

The git checkout command switches between branches or restores files from another commit. When passed a branch name, it switches to that branch.

git checkout <branch>
Enter fullscreen mode Exit fullscreen mode

The checkout command also can be used to discard unstaged changes in the working directory by checking out files by name from the staging area or commits.

8. git reset

The git reset command undoes changes by resetting the current branch to a previous commit. This command works by erasing commits, allowing you to start over from an earlier point.

git reset <commit>
Enter fullscreen mode Exit fullscreen mode

Resetting to a commit erases all history and changes after that point. The reset commit can be identified by Git hash or by relative reference like HEAD~2.

9. git rm

The git rm command removes files from the staging area and working directory. This effectively deletes the file from the project and stops Git from tracking changes to it.

git rm <file>
Enter fullscreen mode Exit fullscreen mode

Like git add, git rm stages a file removal for the next commit. The file is removed from the filesystem and no longer tracked.

10. git stash

The git stash command temporarily stores uncommitted changes for later use, cleaning the working directory. This saves changes without committing them so you can switch branches.

git stash
git stash pop
Enter fullscreen mode Exit fullscreen mode

The stashed changes can be re-applied later with git stash pop. git stash lets you clean up the working directory to handle an urgent bug or task.

11. git merge

The git merge command merges changes from another branch into the current branch. This allows you to combine separate branches of development.

git merge <branch>
Enter fullscreen mode Exit fullscreen mode

Merging integrates the histories of branches together into one unified history. Git attempts to auto-merge changes, but conflicts can occur requiring manual fixes.

12. git pull

The git pull command fetches the newest updates from a remote repository and merges them into the local copy. This command combines git fetch and git merge in one step.

git pull <remote>
Enter fullscreen mode Exit fullscreen mode

Pulling from remotes incorporates other developer's commits into your local repository. This syncs their work with your local copy.

13. git push

The git push command uploads local commits to a remote repository. This allows you to share your code with others and synchronize branch histories.

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

Pushing publishes your local changes and enables other developers to access them from the remote. Remote branches are updated to match the state of local branches after push.

14. git branch

The git branch command lists all branches in the repository. The asterisk denotes the currently active branch.

git branch
Enter fullscreen mode Exit fullscreen mode

This command shows which branch you are working on. You can also use git branch to create new branches locally.

15. git clone

The git clone command makes a copy of a remote repository to your local filesystem. Cloning pulls down the entire project history from a hosted remote.

git clone <url>
Enter fullscreen mode Exit fullscreen mode

Cloning is the easiest way to get started with an existing Git project. It copies the remote repository and sets up a local working copy.

Final Words

Git's broad utility stems from just a handful of core commands that enable version control workflows. Mastering essential commands like init, add, commit, push, pull, and merge equips you to collaborate on projects using Git.

These top 15 Git commands form the basis of version control operations for individual developers and teams. Learning them unlocks the full power and benefits of Git-based collaboration.

Frequently Asked Questions

Here are some common questions about essential Git commands:

How do I initialize a Git repository?

Use the git init command in the directory you want to track. This creates the necessary .git files.

If I make changes, why do I have to use git add before committing them?

Git stages changes before commit. Running git add marks files with changes to be included in the next snapshot commit.

How do I know what files I changed before committing them?

The git status command lists working directory changes, split into untracked, modified, and staged files.

How do I discard uncommitted changes in my working directory?

The git checkout command can restore files in your working tree to their state in another commit or the staging area, discarding edits.

What's the difference between git merge and git rebase?

git merge preserves a linear history while git rebase replays commits onto another base branch, resulting in a different history.

If I mess up my local repository, how can I reset it to a previous state?

The git reset command resets the current branch pointer to an older commit, discarding subsequent changes and commits.

How do I get the latest changes from a remote repository?

The git pull command fetches the latest commits and merges them into your local repository and working tree.

After committing locally, how do I share my changes on a remote repository?

Use the git push command to upload your local commits to a specified remote repository, synchronizing branch states.

Top comments (0)