Git command Cheat Sheet [One STOP Solution]


GIT

Author: Steve Alila
Reviewer: Deepak Prasad

If you are tired of skimming multiple website pages and books or watching lengthy YouTube videos to find a git command, we got your back. This git command cheat sheet presents you with 25 commonly used commands arranged in alphabetical order.

git command cheat sheet

You will learn how to use them with various flags in several scenarios. It would be best to bookmark this tutorial as reference material, speeding up your software development processes.

 

Understand git workflow before going through the git command cheat sheet

Git, in technical terms, is distributed version control system. Think of it as a binary tree to add new changes and retreat your steps.

Working on the terminal involves three primary git workflow levels: working directory, index, and history. The working directory is where you actively modify the files. Index, also called the staging area, is the intersection of the working directory and history.

At the index, git snapshots the changes, waiting for you to undo them or permanently store them in the git database. Each permanently stored change becomes part of the history and is identifiable with a commit hash.

You can remotely track the changes on a cloud-based service, such as Bitbucket, GitLab, or GitHub. In a collaborative workflow, you may include sub-workflows called branches.

Several commands are applicable in the above lines of file tracking. Here is a typical git command cheat sheet.

 

25 most popular git commands

1. git add

Git add stages one or many files depending on the options given. You can stage one file using

git add <file>

or many files using

git add .
git add *

 

2. git branch

Git branch is crucial in listing or deleting branches. To list the branch(es), use

git branch

for local branches,

git branch -r

for remote branches,

git branch -a

for local and remote branches

Delete a branch using

git branch -d <branch name>

 

3. git checkout

How to git remove commit PROPERLY [Practical Examples]

Git checkout command helps to switch between branches. You can also use it to undo changes in a file or commit.

Git command Cheat Sheet [One STOP Solution]

 

4. git clean

Git clean enables you to view or discard untracked files or folders depending on the given flag. For instance,

git clean -n

lists untracked files about to get deleted,

git clean -f

clears all untracked files,

git clean -i

interactively removes all untracked files, while

git clean -fd

deletes both untracked files and directories.

Use

git clean -fX

to delete ignored and untracked files and

git clean -fx

to remove both unignored and ignored files.

 

5. git clone

Git clone repository

Git clone

git clone <remote URL>

enables you to create a copy of a remote repo locally.

Git command Cheat Sheet [One STOP Solution]

 

6. git commit

git commit amend PROPERLY [Easy Examples]

Git commit is one of the most crucial commands we had to include in this git command cheat sheet. Depending on the provided flag, it saves changes in the history.

For instance, -m flag symbolizes message, --all flag saves modified and deleted files. Here is an example of a git commit message with the -m flag:

git commit -m "Feat: Add a text file"

 

7. git fetch

git fetch workflow explained [With Easy Examples]

It transfers remote branch refs, commits, and tags into your local project.

Running the fetch command with a remote name

git fetch <remote>

gets all the repo's branches, downloading all the needed files and commits from the remote. You can specify the remote branch to get a more condensed answer.

git fetch <remote> <branch>

If you want all remotes with their respective branches, use

git fetch --all

Use

git fetch --dry-run

to output actions to be taken without applying the actions.

Lastly, using

git fetch -v

shows all the fetched commits.

 

8. git init

Decoding git init for you and when to use it?

The init command instantiates a repo. The level of interaction with the repo depends on the supplied flags. For example, the --bare flag

git init --bare

omits the working directory, keeping the files in the <project.git>

 

9. git log

Running the git log commands, such as

git log
git log --oneline
git log --pretty=oneline

reveals the state of the commit history.

 

10. git ls-files

git ls-files

lists all the tracked files.

 

11. git ls-remote

Git ls-remote is one of the alternatives to git branch -r.

git ls-remote --heads <remote name>

It lists all remote branches.

Git command Cheat Sheet [One STOP Solution]

 

12. git merge

git merge explained with simple examples [Beginners]

git merge <branch>

takes changes from the feature branch and adds them to the main one, whereas

git merge --squash <branch>

groups feature branch's commits before combining the feature branch's changes on the main branch's workflow.

 

13. git pull

git pull synchronizes a local repo with its upstream by downloading the most updated state of the repo

git pull command examples [5 Methods]

 

14. git push

git push is the opposite of git pull. It synchronizes a remote repository by uploading the most updated state of the local repository.

git push explained with practical examples [Beginners]

 

15. git rebase

Git rebase explained in detail with examples

Git rebase

Git rebase <branch>

creates a linear repo by stacking the changes from the feature branch onto the main branch's commits.

 

16. git reflog

The reflog command

git reflog

reveals all updates made at each branch's tip. It mainly works with the checkout command to restore files you deleted (a few hours ago) using the hard reset command.

 

17. git reset

A practical guide to git reset hard vs soft vs mixed

Git reset takes three forms: hard, soft, mixed. Here is the example of each command in action.

The hard reset on a commit hash

git reset --hard <commit hash>

removes a commit, then deletes the untracked files from the working directory.

A soft reset

git reset --soft <commit hash>
git reset --soft HEAD^
git reset --soft HEAD~1

changes the commit HEAD's reference, taking the changes to the index. Lastly, the mixed reset command on a file

git reset HEAD <file>

unstages the file, keeping the changes in the working directory.

 

18. git restore

The restore command in combination with the --staged flag

git restore --staged <file>

unstages a modified file, whereas

git restore <file>

deletes untracked files.

 

19. git remote

git remote set-url to change remote repo URL [With Examples]

Remote is the name of your repository on a cloud service. You can add it as follows:

git remote add <remote-name> <url>

To delete the remote use

git remote rm <remote-name>

or

git remote remove <remote-name>

and

git remote rename <old-name> <new-name>

to rename a remote.

Lastly, introducing the -v flag

git remote -v

lists the remotes.

 

20. git rm

Git remove command

git rm --cached <file>

unstages a new file.

 

21. git revert

git revert to previous commit [Practical Examples]

Git revert

git revert <commit hash>

undoes operations then creates a new commit, inversing the specified changes.

 

22. git stash

git stash explained in detail with examples

The stash command

git stash

discards the uncommitted changes, recording the discarded changes in the .git/refs/stash file.

You can view the stashed files using the show subcommand.

git stash show

Another commonly applied subcommand is apply

git stash apply

which restores the discarded changes.

 

23. git status

The git status command inspects the staging condition of files. For example, files marked in red are untracked, whereas those marked in green are indexed.

Git command Cheat Sheet [One STOP Solution]

 

24. git switch

Git switch enables you to enter or leave a branch.

git switch <branch name>

You can also use it to create a branch and check out the branch by combining it with the -c.

git switch -c <branch name>

 

25. git tag

You can create a lightweight tag using the command

git tag <tag name>

and

git tag -a <tag name> -m <message>

for an annotated tag.

To list the tags in alphabetical order, use git tag only.

git tag

 

Conclusion

An updated, straightforward git command cheat sheet is one of the most necessary reference materials for a software developer. Use it to improve the efficiency of your software development.

 

Steve Alila

Steve Alila

He specializes in web design, WordPress development, and data analysis, with proficiency in Python, JavaScript, and data extraction tools. Additionally, he excels in web API development, AI integration, and data presentation using Matplotlib and Plotly. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment