DEV Community

Cover image for Commonly Used Git Commands
ak
ak

Posted on

Commonly Used Git Commands

In today's tech-savvy world, mastering Git is almost a rite of passage for developers. Git is a distributed version control system that helps track changes in source code during software development. Whether you're a newbie or a seasoned developer, knowing the most commonly used Git commands can save you a lot of headaches. Let's dive into some essential Git commands that every developer should know.

Setting Up Git

First things first, let's set up Git on your machine. If you haven't installed Git yet, you can download it from Git's official website. After installing, configure your Git with your username and email:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Enter fullscreen mode Exit fullscreen mode

Basic Git Commands

1. git init

The git init command is used to initialize a new Git repository. This command sets up all the necessary files and directories for your project to be tracked by Git.

git init
Enter fullscreen mode Exit fullscreen mode

2. git clone

The git clone command is used to copy an existing Git repository from a remote server to your local machine.

git clone https://github.com/username/repository.git
Enter fullscreen mode Exit fullscreen mode

3. git status

The git status command shows the current state of your working directory and staging area. It tells you which changes have been staged, which haven't, and which files aren't being tracked by Git.

git status
Enter fullscreen mode Exit fullscreen mode

4. git add

The git add command adds changes in the working directory to the staging area. This is the first step in the process of committing changes to the repository.

git add filename
git add .
Enter fullscreen mode Exit fullscreen mode

5. git commit

The git commit command captures a snapshot of the project's currently staged changes. This snapshot is then stored in the project’s history.

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

6. git push

The git push command is used to upload local repository content to a remote repository. This command sends your commits to the remote repository.

git push origin branch-name
Enter fullscreen mode Exit fullscreen mode

7. git pull

The git pull command fetches and integrates changes from a remote repository to your local repository. It is a combination of git fetch and git merge.

git pull origin branch-name
Enter fullscreen mode Exit fullscreen mode

8. git branch

The git branch command is used to list, create, or delete branches. Branching is a powerful feature that allows you to diverge from the main line of development and continue to work without messing with that main line.

git branch
git branch new-branch
git branch -d branch-name
Enter fullscreen mode Exit fullscreen mode

9. git checkout

The git checkout command is used to switch between branches or restore working tree files.

git checkout branch-name
git checkout -b new-branch
Enter fullscreen mode Exit fullscreen mode

10. git merge

The git merge command integrates changes from one branch into another. It’s typically used to combine feature branches back into the main branch.

git merge branch-name
Enter fullscreen mode Exit fullscreen mode

Advanced Git Commands

1. git stash

The git stash command temporarily shelves changes you've made to your working directory. This is useful if you need to switch branches but want to save your changes for later.

git stash
git stash pop
Enter fullscreen mode Exit fullscreen mode

2. git rebase

The git rebase command is used to reapply commits on top of another base tip. It’s a powerful way to rewrite commit history in a linear fashion.

git rebase branch-name
Enter fullscreen mode Exit fullscreen mode

3. git log

The git log command shows the commit history for the repository. It's useful for reviewing and finding specific commits.

git log
Enter fullscreen mode Exit fullscreen mode

4. git diff

The git diff command shows the differences between various commits, branches, files, etc. It’s essential for reviewing changes before committing them.

git diff
git diff branch-name
Enter fullscreen mode Exit fullscreen mode

5. git tag

The git tag command is used to create, list, and delete tags. Tags are used to mark specific points in a repository's history.

git tag
git tag -a v1.0 -m "Version 1.0"
git push origin v1.0
Enter fullscreen mode Exit fullscreen mode

Practical Tips

I remember my early days of using Git; I was often intimidated by its vast array of commands. However, with practice, I began to appreciate its power and flexibility. One memorable instance was when I accidentally deleted an important branch. Thanks to Git's reflog command, I was able to recover it and avoid a major setback. It taught me the importance of learning recovery commands and always keeping backups.

Key Takeaway

Always make small, frequent commits with clear messages. This practice not only helps in keeping track of changes but also makes it easier to revert to previous states if something goes wrong.

Conclusion

Mastering Git commands is an essential skill for any developer. With these commonly used commands, you can effectively manage your projects and collaborate with others. Remember, practice is key. The more you use Git, the more comfortable you'll become with its commands.

Git Branching Strategy Guide


"The best way to predict the future is to invent it." - Alan Kay

Feel free to ask any questions or share your own Git tips in the comments!

Top comments (0)