Mastering Git: 20 Vital Commands Every Developer Should Know

Mastering Git: 20 Vital Commands Every Developer Should Know

In this blog, we’ll look at the top 20 Git commands you should know. These commands help you manage your code and keep everything organized. By using them often, your work will become easier and more efficient. Now, let’s dive in.

1. git init

This command is used to create a local repository. This will create a hidden folder named .git, which includes all the necessary files, objects, and metadata for the repository.

git init

Have you thought about moving your local project to Git? Give it a try now!

2. git clone

This is one of the first command we use, when we work with git projects. This command will create a copy of an existing Git Repository to your local machine. git clone will download all the contents of the Git Repository, including its entire history and metadata.

git clone path_to_your_repo
git clone https://github.com/dotsperpixels/helloworld.git

Typically, the repository path will be a remote URL, but you can also clone from a local repository.

3. git config

This command is used to configure your git repository. Using the git config command, we set name and email for the repository.

git config user.name "Dots Per Pixel"
git config user.email "hello@dotsperpixel.com"

The command above will set the name and email for a specific repository. To set them globally for all repositories, use the --global flag.

git config --global user.name "Dots Per Pixel"
git config --global user.email "hello@dotesperpixel.com"

For system-wide settings (across all users), you can use the --system flag instead of --global.

4. git branch

Git branch command is used to manage a git branch. With this command, you can list, create, and delete branches in a repository.

# To list branches
git branch

# To create a new branch
git branch new-branch

# To remove a branch
git branch -d branch-name
  • -D : Used to forcefully delete a git branch.
  • -m : Used to rename a branch

5. git checkout

The git checkout command is used to switch between different Git branches. It can restore files or branches to their previous state.

# To checkout to develop branch
git checkout develop

# To restore a file to previous state.
git checkout app.py

# To restore the repository to previous committed state
git checkout .

You can also create and switch to a new branch using the git checkout command.

# To create and checkout to feature_branch
git checkout -b feature_branch

6. git status

This command will show the status of the current working directory and the staging area. This will include.

  • Branch information.
  • Information on files or directories that have been added, removed, or modified.
  • Stage changes: Changes that have been added to the staging area and are ready to be committed.
  • Unstaged changes: Changes that are not yet added to the staging area.
git status

7. git add

To add changes from the local repository to the staging area. Before committing a change, we need to add them into the staging area.

# To add a single file or filer
git add filename/foldername

# To add multiple files
git add app.py main.py

# To add all the changes
git add .

# To add a certain pattern of file
git add *.py

If there are certain folders or files you don’t want to include in the remote repository, you can list them in the .gitignore file.

8. git commit

Git commit command will takes the staged snapshot and commits to the project history.

git commit -m "Commit message to identify the change."

In order to commit changes, we need to add the files/ folders in the staging area(using the add command).

9. git push

The git push command is used to upload your local repository commits to a remote repository, synchronizing your local branch with the remote branch.

# Push to the default remote branch
git push

# Push to the specified branch
git push origin master

10. git pull

The git pull command is used to update your local repository with changes from a remote repository. It fetches and merges changes from the remote branch into your current branch.

# To Pull from the default branch or current branch
git pull

# To pull from a specific branch
git pull origin master

11. git log

The git log command is used to view the commit history of a repository. Here, the most recent commit will be listed first.

# shows the recent commit logs
git log

# Shows 10 most recent commits
git log -n 10

# To search a certain commit
git log --grep="mailchimp"

12. git diff

The git diff command is used to display the differences between two commits or to show changes between the working directory and the staging area. We can also use this command to compare two branches as well.

# To compare the current working directory
git diff

# To compare a particular file
git diff path/to/yourfile

# To compare branches or commit
git diff develop master

13. git fetch

This command is used to download update from remote repository, this won’t modify your working directory or the staging area. This will collect the latest commits, branches, tags and reference to the local git repository.

git fetch

# To fetch from a specific remote branch
git fetch remote_master

# To fetch all the branches and tags from remote
git fetch --all

14. git merge

Git merge command is used to merge one branch into another. By doing git merge it will combines histories of two branches and creates a new commit that combines changes from both branches. This is commonly used to transfer changes from feature branches into the development or production branches.

# To merge current branch with another branch
git merge branch_name

15. git remote

The git remote command is used to manage the remote repositories associated with your local repository. Using this command we can add new remote repositories, remove them, or rename them.

# To list all the remote repo
git remote

# To add a remote origin to the current repository
git remote add origin origin_url

16. git revert

This is one of the handy command to rollback or undo a change that is being already committed.

git revert commit-hash

17. git rm

This command is used to remove a file from both the current working directory as well as the git staging area of the repository.

git rm sample.py

18. git stash

The git stash command allows you to temporarily save your current changes without committing them. This is a handy command when you’re working on multiple projects and need to switch contexts quickly.

# For stashing the current changes
git stash

# To list stashes
git stash list

# For applying stashes to the current branch
git stash apply stash@{2}

# Apply the most recent stash to the current working direcotry
git stash pop

19. git tag

This command is used to manage tags in Git, allowing you to mark different versions or releases in your project’s history.

# List tags
git tag

# Add a tag to the current repo
git tag 1.0.1

# To remove a specific tag from current repo
git tag -d 1.0.2

One can create multiple tags for the commit.

20. git cherry-pick

The git cherry-pick command lets you apply changes from a specific commit to your current branch. It’s useful when you want to bring in changes from another branch without merging the whole branch.

git cherry-pick commit-hash

Conclusion

We’ve covered some of the key git commands that can make your workflow easier. There are many more git commands and features to explore. Let us know any missing commands from this list, that you have found useful on a daily basis in the comments.

Credits :
Photo by Roman Synkevych on Unsplash



Leave a Reply