Skip to content

ahmad-mirzaei/git-commands-and-description

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 

Repository files navigation

Git Commands & Description

Description

Git commands are stored in this directory along with their brief descriptions. We welcome translations into other languages.



List of commands

Row Title Row Title Row Title Row Title
1 Init 7 Log 13 Rebase 19 Reset
2 Status 8 Blame 14 Stash 20 Revert
3 Add 9 Show 15 Diff 21 Bisect
4 Rm 10 Config 16 Checkout 22 Upload Project
5 Commit 11 Branch 17 Restore
6 Tag 12 Merge 18 Clean

git-init

commands descriptions
git -v or git --version Check the installed version of Git
git init introducing the project to Git or initializing it
git or git help Show the general Git help

πŸ‘†list of commands


git-status

commands descriptions
git status Viewing the current status of project files
git status --short View the summary of the current project file status
git status -s View the summary of the current project file status

πŸ‘ˆprevious   |   list of commandsπŸ‘†


git-add

commands descriptions
git add [file-name] it takes the selected file to the staging area
git add . it takes all files and project changes to the staging area
git add -A it takes all files and changes of the project to the staging area, just like the previous command
git add *.AnExtension -> git add *.css it takes all of the files with the selected extension to the staging area

πŸ‘ˆprevious   |  list of commandsπŸ‘†


git-rm

commands descriptions
git rm [file name] or git rm -r [file name] it removes the selected file from the project
git rm -- *.AnExtension -> git rm -- *.py it removes all files that have the selected extension from the project
git rm --cached --ignore-unmatch *.js it ignores all the files with the selected extension and takes it to the [working directory]
git rm --cached [file name] -> git rm --cached . it takes the file or all the files that have gone to the staging area to the [working directory]
git rm -rf --cached . it tkes all the files from the local repository to working directory

πŸ‘ˆprevious   |   list of commandsπŸ‘†


git-commit

commands descriptions
git commit -m [message] it takes staging area changes to the repository with a related message
git commit -am [message] or git commit -a -m [message] it takes the files that are already created in the project (and the files which are chainging) directly to the repository; But if a file is newly created, it must first be added to the staging area and then added to the repository with the suitable commit
new file --> git add "file name" --> git commit -m "message"
modified file --> git commit -am "message" --> git commit -a -m "message"
git commit --amend -m [message] Renaming the last commit
git commit --amend -am [message] or git commit --amend -a -m [message] it adds the last working directory changes to the last commit

πŸ‘ˆprevious   |   list of commandsπŸ‘†


git-tag

commands descriptions
git tag or git tag -l Displays the list of tags in the project
git tag [tag name] -> git tag 3.12.0 if we put a version or a special name in front of the command, it will be considered for the last commit
git tag 3.13.0 [commit-hash] -> git tag v5.2.0 6d2ef95 By putting the version or a name for the tag plus the hash coad of the selected commit, we can tag our commits
git show [tag name] -> git show 3.13.2 Viewing the changes in every tag
git tag -d [tag name] -> git tag -d 3.12.2 Removing the tag
git tag -f [tag name] [commit-hash] -> git tag -f v2.1.1 6d2ef95 Removing a tag name from one comit and put the same name for another comite
git tag -l '13.*' - git tag -l '*.0' - git tag -l '*.1.*' Searching in tags

πŸ‘ˆprevious   |   list of commandsπŸ‘†


git-log

commands descriptions
git log it returns the changes and commits of the project
git log --oneline it returns the changes and commits of the project in a summarized and short form
git log --oneline --all viewing all commits and seeing the position of origin and head
git log --stat it reverts the project changes completely
git log --graph it returns the commit information graphically
git log --graph --oneline it returns the commits in a graphic , detailed and summeraized way
git log --after="25-10-12" it returns the commits after a specified date
git log --before="25-10-12" it returns the commits before a specified date
git log --author="user-name" it returns the commits of the selected author or user along with the date and time of their insertion

πŸ‘ˆprevious   |   list of commandsπŸ‘†


git-blame

commands descriptions
git blame [file name] Viewing the author's name of the codes in the file, the date of entry, and the commit time
git blame -e [file name] Viewing the author's email of the codes in the file, the date of entry, and the commit time
git blame [file name] -L [start-line],[end-line] Viewing the author's details of the code in a file, from one line to another
git blame -L [start-line],[end-line] [file-name] It is similar to the previous command with a slight rearrangement of the instructions

πŸ‘ˆprevious   |   list of commandsπŸ‘†


git-show

commands descriptions
git show Viewing all changes in the latest commit
git show [commit-hash] Viewing the changes in the specific commit using its commit hash
git show [commit-hash] --stat It displays the changes made in the specific commit in a summarized form along with the author's details
git show [commit-hash] --path Viewing the actual differences made in the specific commit
git show [tag name] Viewing the changes within each tag

πŸ‘ˆprevious   |   list of commandsπŸ‘†


git-config

commands descriptions
git config --list It displays a list of all the configuration settings currently present in your installed version of Git, such as local settings, global settings, and system settings
git config --local alias.[name] [command-name] -> git config --local alias.com commit With this command, we can choose shorter and more concise names for Git commands in our local environment
git config --global alias.com commit If we want to use an alias across all our projects, we use the word global instead of local, like in this command where we have shortened commit to com
git config --global user.name [your-name] & git config --global user.email [your-email] Setting the username and email for all projects
git config --global user.name & git config --global user.email Viewing the username or email that we have selected for all projects
git config user.name [your-name] & git config user.email [your-email] Setting the username or email for a specific project
git config user.name or git config user.email Viewing the username or email that we have selected for a specific project

πŸ‘ˆprevious   |   list of commandsπŸ‘†


git-branch

commands descriptions
git branch Viewing the names of the project's branches and the branch we are currently on
git branch -a Viewing the list of all branches, including both local and remote branches
git branch origin --delete [branch-name] Deleting a remote branch
git branch [new-branch] Creating a new branch
git switch [branch-name] Switching from one branch to another
git switch -c [new-branch-name] Creating a new branch and switching to it
git branch -d [branch-name] Deleting a branch that has had no changes made to it after creation
git branch -D [branch-name] It is used to delete a branch where changes and commits have been made. You should not be on the branch you want to delete; instead, you need to switch to another branch and then delete the previous branch
git branch -m [new-name] or git branch -m [branch-name] [new-name] Renaming the desired branch. The second method is used when, for example, we are on branch A and want to rename branch B. In this case, there's no need to switch to branch B

πŸ‘ˆprevious   |   list of commandsπŸ‘†


git-merge

commands descriptions
git merge [feature-branch] Merging a feature branch into the main branch; to perform the merge, you need to be on the main branch, and after completing the operation, you should delete the feature branch
git merge [feature-branch] [main-branch] Merging without Switching to the Main Branch

πŸ‘ˆprevious   |   list of commandsπŸ‘†


git-rebase

commands descriptions
git rebase [feature-branch] Merging a feature branch into the main branch
To merge a feature branch into the main branch, you must first switch to the main branch and then use this command
git rebase [feature-branch] [main-branch] Merging a feature branch into the main branch without switching to the main branch

How rebasing and merging work in branch integration


merge-vs-rebase

Comparison of rebase and merge


Merge Rebase Feature
❌ βœ”οΈ Modifies history
βœ”οΈ βœ”οΈ Merges branches
(merge commit) βœ”οΈ ❌ Additional commits
May make branch history complex ❌ βœ”οΈ Keeps history clean
Safer βœ”οΈ Risky (requires caution) 🚨 Team collaboration

🟒 When to use Rebase: When you want a clean history without extra commits.
🟒 When to use Merge: When working in a team environment and you don't want to rewrite history.

πŸ‘ˆprevious   |   list of commandsπŸ‘†


git-stash

commands descriptions
git stash or git stash save You may want to make some changes in a branch that you plan to add to the project later. For this, we use stash, which acts as a holder or a draft
git stash save [message] When using stash, you can provide a name or message to describe the changes being saved
git stash show Viewing General Information About a File That Has Been Stashed
git stash list Viewing the List of Stashes
git stash show stash@{index} Viewing the full changes of any stash using its index.
git stash show -p stash@{index} Viewing the changes of a specific stash in more detail
git stash pop It returns the last item in the stash
git stash apply It applies the changes from the selected stash to the project without removing it from the stash list
git stash drop stash@{index} Delete the specific stash
git stash clear Delete all stashes.

πŸ‘ˆprevious   |   list of commandsπŸ‘†


git-diff

commands descriptions
git diff Viewing or comparing working directory changes with staging area
git diff --name-only [branch-name] Viewing the names of files that have changed after a specific commit
git diff --staged Viewing or comparing the latest commit along with changes in the staging area
git diff head or [HEAD] Comparing the latest commit with the working directory
git diff [first-hash-commit] [second-hash-commit] -> git diff 28344dc 85d9a5b or git diff 28344dc..85d9a5b Viewing or comparing the changes between two commits
git diff 28344dc..85d9a5b [file-name] Viewing or comparing two commits in a file
git diff [first-branch] [second-branch] or git diff [first-branch]..[second-branch] Viewing or comparing the changes between two branches before they are merged
git diff HEAD^ HEAD Displaying the differences between the latest commit and the previous commit

πŸ‘ˆprevious   |   list of commandsπŸ‘†


git-checkout

commands descriptions
git checkout [branch-name] You switch to the branch you have specified
git switch [branch-name] You switch to the branch you have specified
git checkout [hash-commit] When we want to bring the changes of the project to a specific commit in the past, we use this command. In this case, we say the HEAD is detached
git checkout -- [file-name] This command cancels the changes made to the files in the working directory and restores them to the last commit state
git checkout [hash-commit] [file-name] Reverting the changes of a specific file to a commit using its commit hash
git checkout HEAD~[number] -> git checkout HEAD~5 Going back to a few commits earlier by specifying the number of moves from HEAD to previous commits
git checkout HEAD [file-name] Reverting the current changes of a file to the last commit made
Discard working directory changes
git checkout HEAD . When changes are made to several files in the working directory, but we don't want those changes to be applied, we use this command to discard all the changes
Discarding all changes in the working directory
git checkout -- . Discarding all changes in the working directory and reverting it to the state of the last commit
git checkout -b [new-branch-name] Creating a branch and switching to it

πŸ‘ˆprevious   |   list of commandsπŸ‘†


git-restore

commands descriptions
git restore [file-name] When we make changes to a file in the working directory, running this command reverts the changes to the last commit or HEAD, effectively discarding them
git restore --staged [file-name] When we create a new file and add it to the staging area, running this command will unstage it, returning it to an untracked state
git restore --source [hash-commit] [file-name] This command is similar to the following command: git checkout [commit-hash] [file-name]; However, the difference is that the HEAD is not moved or detached. Essentially, we are telling Git to revert the specified file to a particular commit hash or source
git restore --source HEAD [file-name] Reverting changes to the last commit or HEAD
git restore --source HEAD~5 [file-name] Moving the HEAD to five commits back (this will revert the project changes to the state it was in five commits ago).

πŸ‘ˆprevious   |   list of commandsπŸ‘†


git-clean

commands descriptions
git clean -h clean View command help
git clean -f -d [file-name] Deleting a file that has been created but has not yet been staged or committed

πŸ‘ˆprevious   |   list of commandsπŸ‘†


git-reset

commands descriptions
git reset --soft [hash-commit] By running this command, if the specified commit hash corresponds to four commits below, we go back to four commits earlier, and all the commits above it are removed from the repository. However, the files and changes made to the project are not deleted; instead, they are moved back to the staging area, allowing us to decide on them again
git reset [hash-commit] or git reset --mixed [hash-commit] It works similarly to the previous command, meaning it goes back to the previous commit and removes the commits above it from the repository. However, the difference is that if the changes are in the staging area, they are moved to the working directory, and if they are in the working directory, they become untracked
git reset --hard [hash-commit] The entire project is reset to the specified commit, including the working directory. This means all changes revert to the state of the chosen commit
Note: Untracked files are not removed by this command because Git does not recognize them. Therefore, no changes are applied to these files

πŸ‘ˆprevious   |   list of commandsπŸ‘†


git-revert

commands descriptions
git revert [hash-commit] This command is used when, for example, we have created a commit and later realize that it is not useful for the project. It allows us to remove all the code added in that commit and restore the code that was previously deleted.
Important Note: If we revert a commit that depends on another commit, we may encounter a conflict. To resolve this issue, we should use the reset command instead

πŸ‘ˆprevious   |   list of commandsπŸ‘†


git-bisect

commands descriptions
git bisect start When the project has many commits and we encounter a bug, we use this command to find and fix the bug. This command performs a binary search through the commits to locate the problematic commit
git bisect good [hash-commit] To use binary search in commits, we first need to mark a commit where the bug did not exist. Then, we manage the search by marking commits as "good" or "bad" until we locate the commit that introduced the bug
git bisect bad We manage the search by marking commits as "good" or "bad" until we reach the commit that introduced the bug
git bisect reset After reaching the problematic commit and fixing the bug, this command is used to conclude the process

πŸ‘ˆprevious   |   list of commandsπŸ‘†


upload-project

commands descriptions
git clone [https-repository-link] Adding a copy of a repository to our local machine
echo "# git-review" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin [repository-link]
git push -u origin main
When we want to start a project from scratch and add it to Git and GitHub, these steps must be followed
git remote add origin [repository-link]` <br /> `git branch -M main` <br /> `git push -u origin main When the project has been worked on and added to Git, and only the final steps remain, these are the steps you need to follow
git remote This command helps to determine which remote service the project is connected to
git remote -v If we want more information about the remote, we use this command
git remote remove [remote-name] Removing the specified remote
git push [remote] [branch] The action of uploading the project's code to GitHub is called "push." To push the project to GitHub, we specify the remote name we have selected and the branch name to push the changes from that branch to the chosen remote
git push -u origin [main-branch] By running this command, the remote and branch are set for us, so there is no need to write them again to upload changes to GitHub. For future uploads, you can simply use the command: git push
git pull origin [main-branch] Bringing changes from the GitHub repository to the local project
git fetch [remote-name] [branch-name] When changes have been made by ourselves or colleagues on GitHub, and we want to review those changes before merging, we use this command. After running it, we won't see any specific output because the changes have gone to the remote branch. To view them, we use this command: git branch -r
git branch -r It is used to view remote branches
git switch -c [branch-name] origin/main
or
git checkout -b [branch-name] origin/main
To view changes in a remote branch, you first need to create a new branch, observe the changes there, then merge it. Finally, after merging, delete the merged branch. For this, we will use the following command

πŸ‘ˆprevious   |   list of commandsπŸ‘†