When it comes to software development, version control is essential. It allows you to track your code changes, revert to previous stages, and collaborate with your team on a project. One of the most popular version control systems is Git. Whether you’re a beginner just starting out or an experienced developer looking to streamline your workflow, understanding Git commands is a skill that will undoubtedly pay off.
In this post, we will delve into 30 basic Git commands that every developer should know. These commands will help you initialize a repository, make commits, create and switch branches, and much more. By mastering these commands, you’ll be well on your way to becoming a more efficient and effective developer.
.no-js #ref-block-post-22344 .ref-block__thumbnail { background-image: url(“https://assets.hongkiat.com/uploads/thumbs/250×160/github-overlooked-features.jpg”); }
10 Useful Github Features to Know
Github is now the place where programmers and designers work together. They collaborate, contribute, and fix bugs. It… Read more
1. git init:
This command is used to initialize a new Git repository. It creates a new .git subdirectory in your current working directory. This will also create a new branch named master.
Example:
git init
This will initialize a Git repository in your current directory.
2. git clone:
This command is used to clone a repository. It creates a copy of a remote repository on your local machine.
Example:
git clone https://github.com/username/repository.git
This will clone the repository at the given URL to your local machine.
3. git add:
This command adds a file to the staging area in preparation for a commit.
Example:
git add filename
This will add the file named “filename” to the staging area.
4. git commit:
This command is used to save your changes to the local repository. It takes a snapshot of the changes you’ve staged using git add.
Example:
git commit -m "Commit message"
This will commit your changes with a message describing what you changed.
5. git status:
This command shows the status of changes as untracked, modified, or staged.
Example:
git status
This will display the status of your working directory.
6. git pull:
This command fetches changes from a remote repository and merges them into your current branch.
Example:
git pull origin master
This will pull changes from the master branch of the origin remote repository.
7. git push:
This command sends your committed changes to a remote repository.
Example:
git push origin master
This will push your committed changes to the master branch of the origin remote repository.
8. git branch:
This command lists all of the branches in your repository.
Example:
git branch
This will list all of the branches in your repository.
9. git checkout:
This command is used to switch between branches in a Git repository.
Example:
git checkout branch-name
This will switch to the branch named “branch-name”.
10. git merge:
This command merges the changes from one branch into another.
Example:
git merge branch-name
11. git diff:
This command shows the file differences which are not yet staged.
Example:
git diff
This will show unstaged differences since the last commit.
12. git reset:
This command unstages the file, but it preserves the file contents.
Example:
git reset filename
This will unstage the file named “filename“.
13. git rm:
This command deletes the file from your working directory and stages the deletion.
Example:
git rm filename
This will delete the file named “filename” and stage the deletion.
14. git log:
This command shows a listing of commits on a branch including the corresponding details.
Example:
git log
This will display an ordered list of the recent commits.
15. git show:
This command shows the metadata and content changes of the specified commit.
Example:
git show
This will display the metadata and content changes of the latest commit.
16. git tag:
This command is used to give tags to the specified commit.
Example:
git tag v1.0
This will tag the latest commit with “v1.0”.
17. git fetch:
This command fetches all the objects from the remote repository that are not present in the local one.
Example:
git fetch origin
This will fetch all objects from the origin remote that don’t exist in your current repository.
18. git rebase:
This command is used to apply the changes made on the current branch ahead of another branch.
Example:
git rebase master
This will apply any changes made on the current branch ahead of the master branch.
19. git revert:
This command creates a new commit that undoes the changes made in a previous commit.
Example:
git revert HEAD
This will create a new commit that undoes the changes made in the last commit.
20. git stash:
This command temporarily saves changes that you don’t want to commit immediately. You can apply the changes later.
Example:
git stash
This will temporarily save all modified tracked files.
21. git stash pop:
This command restores the most recently stashed changes.
Example:
git stash pop
This will apply the most recently stashed changes and remove them from the stash list.
22. git stash list:
This command lists all stashed changesets.
Example:
git stash list
This will display all stashed changesets.
23. git stash drop:
This command discards the most recently stashed changeset.
Example:
git stash drop
This will discard the most recently stashed changeset.
24. git cherry-pick:
This command applies the changes introduced by some existing commits.
Example:
git cherry-pick commitID
This will apply the changes introduced by the commit with the given ID.
25. git bisect:
This command uses a binary search algorithm to find which commit in your project’s history introduced a bug.
Example:
git bisect start git bisect bad git bisect good commitID
This will start the bisecting process, mark the current commit as bad, and mark the commit with the given ID as good.
26 git blame:
This command shows what revision and author last modified each line of a file.
Example:
git blame filename
This will show what revision and author last modified each line of “filename”.
27. git clean:
This command removes untracked files from your working directory.
Example:
git clean -n
This will show what will be removed without actually doing it. Replace -n
with -f
to actually remove the files.
28 git reflog:
This command shows a list of all references to commits in the local repository.
Example:
git reflog
This will display all references to commits in your local repository.
29. git grep:
This command lets you search through your repository.
Example:
git grep "hello"
This will search the repository for any occurrences of “hello”.
30. gitk:
This command launches the Git repository browser.
Example:
gitk
This will launch the Git repository browser.
Conclusion
In conclusion, Git is a powerful tool that can greatly enhance your productivity and efficiency as a developer. The 30 basic Git commands we’ve discussed in this post are just the tip of the iceberg. There are many more commands and options available in Git, and we encourage you to explore them further.
Remember, practice makes perfect. The more you use these commands, the more comfortable you’ll become with them. So, don’t be afraid to dive in and start using Git in your projects. It may seem daunting at first, but with time and practice, you’ll find that it’s an invaluable tool in your development toolkit.
The post 30 Basic Git Commands You Should Know appeared first on Hongkiat.