{"id":535,"date":"2023-06-12T13:01:00","date_gmt":"2023-06-12T13:01:00","guid":{"rendered":"https:\/\/upprofits.net\/?p=535"},"modified":"2024-08-30T11:29:22","modified_gmt":"2024-08-30T11:29:22","slug":"30-basic-git-commands-you-should-know","status":"publish","type":"post","link":"https:\/\/upprofits.net\/index.php\/2023\/06\/12\/30-basic-git-commands-you-should-know\/","title":{"rendered":"30 Basic Git Commands You Should Know"},"content":{"rendered":"

When it comes to software development, version control<\/a> 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<\/a>. Whether you\u2019re 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. <\/p>\n

\"basic<\/figure>\n

In this post, we will delve into 30 basic Git commands that every developer should know<\/strong>. These commands will help you initialize a repository, make commits, create and switch branches, and much more. By mastering these commands, you\u2019ll be well on your way to becoming a more efficient and effective developer. <\/p>\n

\n\t\t\t\t\t10 Useful Github Features to Know<\/span><\/a><\/p>\n
\n

.no-js #ref-block-post-22344 .ref-block__thumbnail { background-image: url(“https:\/\/assets.hongkiat.com\/uploads\/thumbs\/250×160\/github-overlooked-features.jpg”); }<\/p><\/div>\n

\n

10 Useful Github Features to Know<\/h4>\n

\n\t\t\t\t\t\tGithub is now the place where programmers and designers work together. They collaborate, contribute, and fix bugs. It…\t\t\t\t\t\tRead more<\/span><\/p>\n<\/div>\n<\/div>\n

1. git init:<\/code><\/h5>\n

This command is used to initialize a new Git repository. It creates a new .git<\/em> subdirectory in your current working directory. This will also create a new branch named master.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit init<\/pre>\n

This will initialize a Git repository in your current directory.<\/p>\n

<\/div>\n
2. git clone:<\/code><\/h5>\n

This command is used to clone a repository. It creates a copy of a remote repository on your local machine.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit clone https:\/\/github.com\/username\/repository.git<\/pre>\n

This will clone the repository at the given URL to your local machine.<\/p>\n

<\/div>\n
3. git add:<\/code><\/h5>\n

This command adds a file to the staging area in preparation for a commit.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit add filename<\/pre>\n

This will add the file named \u201cfilename\u201d to the staging area.<\/p>\n

<\/div>\n
4. git commit:<\/code><\/h5>\n

This command is used to save your changes to the local repository. It takes a snapshot of the changes you\u2019ve staged using git add.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit commit -m \"Commit message\"<\/pre>\n

This will commit your changes with a message describing what you changed.<\/p>\n

<\/div>\n
5. git status:<\/code><\/h5>\n

This command shows the status of changes as untracked, modified, or staged.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit status<\/pre>\n

This will display the status of your working directory.<\/p>\n

<\/div>\n
6. git pull:<\/code><\/h5>\n

This command fetches changes from a remote repository and merges them into your current branch.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit pull origin master <\/pre>\n

This will pull changes from the master branch of the origin remote repository.<\/p>\n

<\/div>\n
7. git push:<\/code><\/h5>\n

This command sends your committed changes to a remote repository.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit push origin master <\/pre>\n

This will push your committed changes to the master branch of the origin remote repository.<\/p>\n

<\/div>\n
8. git branch:<\/code><\/h5>\n

This command lists all of the branches in your repository.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit branch<\/pre>\n

This will list all of the branches in your repository.<\/p>\n

<\/div>\n
9. git checkout:<\/code><\/h5>\n

This command is used to switch between branches in a Git repository.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit checkout branch-name<\/pre>\n

This will switch to the branch named \u201cbranch-name\u201d.<\/p>\n

<\/div>\n
10. git merge:<\/code><\/h5>\n

This command merges the changes from one branch into another.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit merge branch-name<\/pre>\n
<\/div>\n
11. git diff:<\/code><\/h5>\n

This command shows the file differences which are not yet staged.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit diff <\/pre>\n

This will show unstaged differences since the last commit.<\/p>\n

<\/div>\n
12. git reset:<\/code><\/h5>\n

This command unstages the file, but it preserves the file contents.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit reset filename <\/pre>\n

This will unstage the file named \u201cfilename<\/em>\u201c.<\/p>\n

<\/div>\n
13. git rm:<\/code><\/h5>\n

This command deletes the file from your working directory and stages the deletion.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit rm filename<\/pre>\n

This will delete the file named \u201cfilename\u201d and stage the deletion.<\/p>\n

<\/div>\n
14. git log:<\/code><\/h5>\n

This command shows a listing of commits on a branch including the corresponding details.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit log <\/pre>\n

This will display an ordered list of the recent commits.<\/p>\n

<\/div>\n
15. git show:<\/code><\/h5>\n

This command shows the metadata and content changes of the specified commit.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit show<\/pre>\n

This will display the metadata and content changes of the latest commit.<\/p>\n

<\/div>\n
16. git tag:<\/code><\/h5>\n

This command is used to give tags to the specified commit.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit tag v1.0<\/pre>\n

This will tag the latest commit with \u201cv1.0\u201d.<\/p>\n

<\/div>\n
17. git fetch:<\/code><\/h5>\n

This command fetches all the objects from the remote repository that are not present in the local one.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit fetch origin<\/pre>\n

This will fetch all objects from the origin remote that don\u2019t exist in your current repository.<\/p>\n

<\/div>\n
18. git rebase:<\/code><\/h5>\n

This command is used to apply the changes made on the current branch ahead of another branch.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit rebase master<\/pre>\n

This will apply any changes made on the current branch ahead of the master branch.<\/p>\n

<\/div>\n
19. git revert:<\/code><\/h5>\n

This command creates a new commit that undoes the changes made in a previous commit.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit revert HEAD<\/pre>\n

This will create a new commit that undoes the changes made in the last commit.<\/p>\n

<\/div>\n
20. git stash:<\/code><\/h5>\n

This command temporarily saves changes that you don\u2019t want to commit immediately. You can apply the changes later.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit stash <\/pre>\n

This will temporarily save all modified tracked files.<\/p>\n

<\/div>\n
21. git stash pop:<\/code><\/h5>\n

This command restores the most recently stashed changes.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit stash pop<\/pre>\n

This will apply the most recently stashed changes and remove them from the stash list.<\/p>\n

<\/div>\n
22. git stash list:<\/code><\/h5>\n

This command lists all stashed changesets.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit stash list <\/pre>\n

This will display all stashed changesets.<\/p>\n

<\/div>\n
23. git stash drop:<\/code><\/h5>\n

This command discards the most recently stashed changeset.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit stash drop<\/pre>\n

This will discard the most recently stashed changeset.<\/p>\n

<\/div>\n
24. git cherry-pick:<\/code><\/h5>\n

This command applies the changes introduced by some existing commits.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit cherry-pick commitID <\/pre>\n

This will apply the changes introduced by the commit with the given ID.<\/p>\n

<\/div>\n
25. git bisect:<\/code><\/h5>\n

This command uses a binary search algorithm to find which commit in your project\u2019s history introduced a bug.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit bisect start\r\ngit bisect bad\r\ngit bisect good commitID<\/pre>\n

This will start the bisecting process, mark the current commit as bad, and mark the commit with the given ID as good.<\/p>\n

<\/div>\n
26 git blame:<\/code><\/h5>\n

This command shows what revision and author last modified each line of a file.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit blame filename <\/pre>\n

This will show what revision and author last modified each line of \u201cfilename\u201d.<\/p>\n

<\/div>\n
27. git clean:<\/code><\/h5>\n

This command removes untracked files from your working directory.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit clean -n <\/pre>\n

This will show what will be removed without actually doing it. Replace -n<\/code> with -f<\/code> to actually remove the files.<\/p>\n

<\/div>\n
28 git reflog:<\/code><\/h5>\n

This command shows a list of all references to commits in the local repository.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit reflog <\/pre>\n

This will display all references to commits in your local repository.<\/p>\n

<\/div>\n
29. git grep:<\/code><\/h5>\n

This command lets you search through your repository.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngit grep \"hello\" <\/pre>\n

This will search the repository for any occurrences of \u201chello\u201d.<\/p>\n

<\/div>\n
30. gitk:<\/code><\/h5>\n

This command launches the Git repository browser.<\/p>\n

Example:<\/strong><\/p>\n

\r\ngitk<\/pre>\n

This will launch the Git repository browser.<\/p>\n

<\/div>\n

Conclusion<\/h4>\n

In conclusion, Git is a powerful tool that can greatly enhance your productivity and efficiency as a developer. The 30 basic Git commands we\u2019ve 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. <\/p>\n

Remember, practice makes perfect. The more you use these commands, the more comfortable you\u2019ll become with them. So, don\u2019t be afraid to dive in and start using Git in your projects. It may seem daunting at first, but with time and practice, you\u2019ll find that it\u2019s an invaluable tool in your development toolkit.<\/p>\n

The post 30 Basic Git Commands You Should Know<\/a> appeared first on Hongkiat<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"

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\u2019re a beginner just starting out or an experienced developer looking to streamline your […]<\/p>\n","protected":false},"author":1,"featured_media":539,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-535","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-coding"],"_links":{"self":[{"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/posts\/535"}],"collection":[{"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/comments?post=535"}],"version-history":[{"count":2,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/posts\/535\/revisions"}],"predecessor-version":[{"id":540,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/posts\/535\/revisions\/540"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/media\/539"}],"wp:attachment":[{"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/media?parent=535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/categories?post=535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/tags?post=535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}