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.
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
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.
This will initialize a Git repository in your current directory.
This command is used to clone a repository. It creates a copy of a remote repository on your local machine.
git clone https://github.com/username/repository.git
This will clone the repository at the given URL to your local machine.
This command adds a file to the staging area in preparation for a commit.
git add filename
This will add the file named “filename” to the staging area.
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.
git commit -m "Commit message"
This will commit your changes with a message describing what you changed.
This command shows the status of changes as untracked, modified, or staged.
This will display the status of your working directory.
This command fetches changes from a remote repository and merges them into your current branch.
git pull origin master
This will pull changes from the master branch of the origin remote repository.
This command sends your committed changes to a remote repository.
git push origin master
This will push your committed changes to the master branch of the origin remote repository.
This command lists all of the branches in your repository.
This will list all of the branches in your repository.
This command is used to switch between branches in a Git repository.
git checkout branch-name
This will switch to the branch named “branch-name”.
This command merges the changes from one branch into another.
git merge branch-name
This command shows the file differences which are not yet staged.
This will show unstaged differences since the last commit.
This command unstages the file, but it preserves the file contents.
git reset filename
This will unstage the file named “filename“.
This command deletes the file from your working directory and stages the deletion.
git rm filename
This will delete the file named “filename” and stage the deletion.
This command shows a listing of commits on a branch including the corresponding details.
This will display an ordered list of the recent commits.
This command shows the metadata and content changes of the specified commit.
This will display the metadata and content changes of the latest commit.
This command is used to give tags to the specified commit.
git tag v1.0
This will tag the latest commit with “v1.0”.
This command fetches all the objects from the remote repository that are not present in the local one.
git fetch origin
This will fetch all objects from the origin remote that don’t exist in your current repository.
This command is used to apply the changes made on the current branch ahead of another branch.
git rebase master
This will apply any changes made on the current branch ahead of the master branch.
This command creates a new commit that undoes the changes made in a previous commit.
git revert HEAD
This will create a new commit that undoes the changes made in the last commit.
This command temporarily saves changes that you don’t want to commit immediately. You can apply the changes later.
This will temporarily save all modified tracked files.
git stash pop:
This command restores the most recently stashed changes.
git stash pop
This will apply the most recently stashed changes and remove them from the stash list.
git stash list:
This command lists all stashed changesets.
git stash list
This will display all stashed changesets.
git stash drop:
This command discards the most recently stashed changeset.
git stash drop
This will discard the most recently stashed changeset.
This command applies the changes introduced by some existing commits.
git cherry-pick commitID
This will apply the changes introduced by the commit with the given ID.
This command uses a binary search algorithm to find which commit in your project’s history introduced a bug.
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.
This command shows what revision and author last modified each line of a file.
git blame filename
This will show what revision and author last modified each line of “filename”.
This command removes untracked files from your working directory.
git clean -n
This will show what will be removed without actually doing it. Replace
-f to actually remove the files.
This command shows a list of all references to commits in the local repository.
This will display all references to commits in your local repository.
This command lets you search through your repository.
git grep "hello"
This will search the repository for any occurrences of “hello”.
This command launches the Git repository browser.
This will launch the Git repository browser.
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.