1
How To

22 Quick Git Commands Every Developer Should Know

6 Mins read

Let’s quickly go through Git Commands Every Developer Should Know. Git is a powerful source control system that is commonly used in open-source software development projects. In this blog post, we’re going to cover 22 basic but useful commands for developers who are new to Git.

1) git init

git init

A new git repository is empty and ready to be worked on.

2) git add -A

git add -A

Adds all changes in the working directory, including deletions. You should not use this command if you have any tracked files that you want to keep track of but exclude from the show change list. This command will add those files as well as any unstaged changes.

3) git diff

git diff

Displays the difference between the index and the working directory. Does not show staged changes.

4) git commit -m “your message”

git commit -m "your message"

Commits a change using a clear, terse message. The single quotes are important so that the shell doesn’t interpret any shell metacharacters like * (for files whose name starts with “f”), ** (for directories with names that start with “d”), etc. Make sure there is a space after your message before you hit enter to send it to the command prompt.

5) git clone

git clone

Clones a git repository from the URL. This will allow you to have a copy of this repository on your local machine.

You can check out other projects by just doing “git clone” for the project name and then working on the project that you just cloned.

6) git status (or git status -s)

git status

Gets a summary of what files have changed since the last commit, if any. If you do not see any changes, perhaps there are no changes to show in the index or maybe you have staged changes that have not been committed yet.

7) git log

git log

Gets a log of all commits since a certain point in time, such as the last commit or the creation of a new branch. Log entries might be shortened or made into a “patch” format for easier reading. This command requires that you specify the date of interest with either –before or –after command-line arguments. You can get a list of all commits with no date by simply doing “git log”.

8) git branch

git branch

Lists the names of all branches in the repository.

9) git push –force

git push --force

This command is used for pushing any changes to the remote repository. It will force push all commits at once, even if there are already changes on the remote. It is often used in combination with ‘git pull’ to quickly update your local and remote repositories in case some change you want to push needs to be pulled before it can be pushed, or you may want to update your remote repository before pushing a new commit (like when a new commit introduces new files that are only required by one other branch).

10) git merge

git merge

Merges changes from another branch into your current development branch. When you are done merging, move your working directory to the new development branch using the “git checkout” command. If you do not have any uncommitted changes to get merged, just do a “git add -A” and commit your changes before doing a “git merge”.

11) git checkout <branch name>

git checkout  <branch name>

Starts working on a new branch with the name of <branch name>.

12) git log –stat

git log --stat

Shows: last commit, total commits, and total files. This is useful if you have the .git directory and you want to see the history of all changes that have been made to a project. It is useful for showing different versions of files in a repository that are in different branches. It is also useful when trying to reproduce an issue or bug report. Make note of what commit shows what version of files/filesystems within the repository.

13) git checkout -b <branch name>

git checkout -b <branch name>

Starts working on a new branch with the name of <branch name>

14) git push -u origin

git push -u origin

Pushes all changes to the remote repository. The ‘-u’ argument is used to specify an upstream. You can supply branches and tags using the ‘-b’ and ‘-t’ arguments, respectively. If you omit one of those two, you will be pushed to the default remote that you have configured for your account in git config. An example: $ git push origin gh-pages –all

You can also just use git pull, which will automatically fetch any changes from the default remotes you have configured for your account in Git.

15) git pull

git pull

Fetches all the changes from the remote repository. You can use the ‘-v’ argument to show verbose output. You can also use it to update both the local and remote repository with a single command.

16) git checkout main

git checkout main

Starts working on main branch, which is equal to the current head of your current development branch, with the current head being HEAD in VCS sense. To make your changes visible in other branches do this push/pull as required after which you need to perform stage and commit in every other branch where these changes are going to be visible.

17) git diff –cached

git diff --cached

Displays the staged changes in your working directory. Use this command to review the changes that you have made that you want to commit. Any changes in the index are not shown here. If you want to see what files are different between your current index and what is currently staged in the working directory, use “git diff” instead. This command will not show any content differences in files that are identical between your index and your working directory.

18) git stash

git stash

Allows you to temporarily save current state of files from your working directory. You might want to do it if you have made some changes and need to revert them back to the way they were before. If you are working on any large project, this command is a good thing to get the switch into different workflow mode. For example, you should use this command when your commit is failing because you did not think through committing these changes, maybe because it is quite large and you need some time to think about what can be done later. With “git stash”, you will be able to undo these changes as well as reset your local repository state back to where it was before cherry-picking.

19) git stash list

git stash list

List all stashes available in the current repository.

20) git stash apply

git stash apply

Applies any outstanding changes in an applied stash. This will also run “git status” to show you what files have changed between the head of your current branch and the last commit in your work tree that you want to re-apply. Once you are positive which files changed and know what branches they are on, use this command. You can also use “git checkout ” to go to a specific commit instead of just going back to a specific point in time, without having to deal with committing again by hand.

21) git show

git show

Shows the history of any tagged or branch-named point. For example, you can check out a previous commit, tag, or branch by doing “git show HEAD” as well as checking out previous commits with additional arguments. You can also do it by specifying reference points like “git show 9c88f8dabc”. However, you should specify the SHA-1 of an existing commit if you are showing content differences between revisions. This is a more accurate way to see what has changed in a revision that you might be interested in versioning.

You can also use this command to see diffs of any specific change within the commit log.

22) git show –stat

git show --stat

Shows: last commit, total commits, and total files. This is useful if you have the .git directory and you want to see the history of all changes that have been made to a project. It is useful for showing different versions of files in a repository that are in different branches. It is also useful when trying to reproduce an issue or bug report. Make note of what commit shows what version of files/filesystems within the repository.

22) git stash pop

git stash pop

Removes the selected stash from your list of stashes.

Conclusion

The above commands and other tools will help you perform Git version control. There are many other commands available in git, but this article covers the most common ones and their uses. However, if you want to learn more, you can read the documentation and see the directory structure of git in your machine. This will help you learn many other commands as well as what they do, which is also important for being able to troubleshoot any errors that may occur when using Git.

You might be interested in how to implement a centralized software development process

Don’t miss amazing tips!

1
Related posts
How ToProgrammingSoftwareTechnology

How to configure Apache Airflow on MacOS locally

4 Mins read
Table of contents0.1 Creating the project folder and the virtual environment0.2 Installing0.3 Running airflow0.4 Starting the webflow server0.5 Starting the scheduler0.6 Running…
Code ChallengesHow ToProgrammingSoftware

How To Implement Merge Sort and Quick Sort Algorithms In Python 3

3 Mins read
Table of contents0.1 Merge Sort0.2 Quick Sort0.3 Conclusion1 Don’t miss amazing tips! Let’s have a look at how to how to implement…
How To

How to write test with Ruby and Capybara with Examples

11 Mins read
Table of contents1 What are the advantages of using Capybara1.1 1) Webdriver agnostic1.2 2) Works with multiple testing frameworks1.3 Capybara DSL1.4 a)…

Leave a Reply

Your email address will not be published. Required fields are marked *

60 − 53 =

×
How ToStartups

19 Great Tips On Creating A Good Landing Page For Your Website