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
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
.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\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
git init:<\/code><\/h5>\nThis 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>\nThis will initialize a Git repository in your current directory.<\/p>\n
<\/div>\n2.
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>\nThis will clone the repository at the given URL to your local machine.<\/p>\n
<\/div>\n3.
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>\nThis will add the file named \u201cfilename\u201d to the staging area.<\/p>\n
<\/div>\n4.
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>\nThis will commit your changes with a message describing what you changed.<\/p>\n
<\/div>\n5.
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>\nThis will display the status of your working directory.<\/p>\n
<\/div>\n6.
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>\nThis will pull changes from the master branch of the origin remote repository.<\/p>\n
<\/div>\n7.
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>\nThis will push your committed changes to the master branch of the origin remote repository.<\/p>\n
<\/div>\n8.
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>\nThis will list all of the branches in your repository.<\/p>\n
<\/div>\n9.
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>\nThis will switch to the branch named \u201cbranch-name\u201d.<\/p>\n
<\/div>\n10.
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>\n11.
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>\nThis will show unstaged differences since the last commit.<\/p>\n
<\/div>\n12.
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>\nThis will unstage the file named \u201cfilename<\/em>\u201c.<\/p>\n
<\/div>\n13.
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>\nThis will delete the file named \u201cfilename\u201d and stage the deletion.<\/p>\n
<\/div>\n14.
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>\nThis will display an ordered list of the recent commits.<\/p>\n
<\/div>\n15.
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>\nThis will display the metadata and content changes of the latest commit.<\/p>\n
<\/div>\n16.
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>\nThis will tag the latest commit with \u201cv1.0\u201d.<\/p>\n
<\/div>\n17.
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>\nThis will fetch all objects from the origin remote that don\u2019t exist in your current repository.<\/p>\n
<\/div>\n18.
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>\nThis will apply any changes made on the current branch ahead of the master branch.<\/p>\n
<\/div>\n19.
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>\nThis will create a new commit that undoes the changes made in the last commit.<\/p>\n
<\/div>\n20.
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>\nThis will temporarily save all modified tracked files.<\/p>\n
<\/div>\n21.
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>\nThis will apply the most recently stashed changes and remove them from the stash list.<\/p>\n
<\/div>\n22.
git stash list:<\/code><\/h5>\n
This command lists all stashed changesets.<\/p>\n
Example:<\/strong><\/p>\n
\r\ngit stash list <\/pre>\nThis will display all stashed changesets.<\/p>\n
<\/div>\n23.
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>\nThis will discard the most recently stashed changeset.<\/p>\n
<\/div>\n24.
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>\nThis will apply the changes introduced by the commit with the given ID.<\/p>\n
<\/div>\n25.
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>\nThis will start the bisecting process, mark the current commit as bad, and mark the commit with the given ID as good.<\/p>\n
<\/div>\n26
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>\nThis will show what revision and author last modified each line of \u201cfilename\u201d.<\/p>\n
<\/div>\n27.
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>\nThis 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>\nThis will display all references to commits in your local repository.<\/p>\n
<\/div>\n29.
git grep:<\/code><\/h5>\n
This command lets you search through your repository.<\/p>\n
Example:<\/strong><\/p>\n
\r\ngit grep \"hello\" <\/pre>\nThis will search the repository for any occurrences of \u201chello\u201d.<\/p>\n
<\/div>\n30.
gitk:<\/code><\/h5>\n
This command launches the Git repository browser.<\/p>\n
Example:<\/strong><\/p>\n
\r\ngitk<\/pre>\nThis will launch the Git repository browser.<\/p>\n
<\/div>\nConclusion<\/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":"