0% found this document useful (0 votes)
29 views

Git Commands

The document lists 85 commonly used Git commands along with examples of each command. Some of the most common commands include git init to initialize a new repository, git add to add files to the staging area, git commit to commit changes to the repository, git status to show the status of the repository, git diff to show differences between commits, and git log to show the commit history. The document provides examples for each command to demonstrate their proper usage.

Uploaded by

banu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Git Commands

The document lists 85 commonly used Git commands along with examples of each command. Some of the most common commands include git init to initialize a new repository, git add to add files to the staging area, git commit to commit changes to the repository, git status to show the status of the repository, git diff to show differences between commits, and git log to show the commit history. The document provides examples for each command to demonstrate their proper usage.

Uploaded by

banu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

List of commonly used Git commands

git init: Initializes a new Git repository.

1. Example: git init


git add: Adds files to the staging area.

2. Example: git add file.txt


git commit: Commits changes to the repository.

3. Example: git commit -m "Added new feature"


git status: Shows the status of the repository.

4. Example: git status


git diff: Shows the differences between the working directory and the last commit.

5. Example: git diff


git log: Shows the commit history of the repository.

6. Example: git log


git checkout: Switches to a different branch or commit.

7. Example: git checkout main


git branch: Shows a list of branches.

8. Example: git branch


git merge: Merges changes from one branch into another.

9. Example: git merge feature-branch


git remote: Shows a list of remote repositories.

10. Example: git remote -v


git clone: Clones a remote repository to the local machine.

11. Example: git clone https://github.com/user/repo.git


git pull: Fetches changes from a remote repository and merges them into the current
branch.

12. Example: git pull origin main


git push: Pushes changes to a remote repository.

13. Example: git push origin main


git stash: Stashes changes in a working directory.

14. Example: git stash

Created by : Eng. Shady Ahmed +201023527158


git tag: Lists or creates tags.

15. Example: git tag -a v1.0.0 -m "Initial release"


git reset: Resets the current branch to a previous commit.

16. Example: git reset HEAD~1


git revert: Reverts a commit.

17. Example: git revert abc123


git cherry-pick: Picks a specific commit from one branch and applies it to another.

18. Example: git cherry-pick abc123


git rm: Removes a file from the working directory and the repository.

19. Example: git rm file.txt


git mv: Renames or moves a file in the working directory and the repository.

20. Example: git mv file.txt newfile.txt


git clone --depth: Clones a repository with a limited number of commits.

21. Example: git clone --depth=1 https://github.com/user/repo.git


git log --oneline: Shows a simplified commit history of the repository.

22. Example: git log --oneline


git log --author: Shows the commit history of the repository by a specific author.

23. Example: git log --author="John Doe"


git log --since: Shows the commit history of the repository since a specific date.

24. Example: git log --since="2022-01-01"


git log --grep: Shows the commit history of the repository with a specific commit
message.

25. Example: git log --grep="fixed typo"


git blame: Shows who last modified each line of a file and when.

26. Example: git blame file.txt


git log --graph: Shows the commit history of the repository in a visual graph.

27. Example: git log --graph


git tag -d: Deletes a tag.

28. Example: git tag -d v1.0.0


git branch -d: Deletes a branch.

29. Example: git branch -d feature-branch

Created by : Eng. Shady Ahmed +201023527158


30. git branch -m: Renames a branch.

git push --force: Forces a push to a remote repository, overwriting its history.

31. Example: git push --force origin main


git fetch: Fetches changes from a remote repository but does not merge them.

32. Example: git fetch origin


git rebase: Reapplies commits on top of another base tip.

33. Example: git rebase main


git reflog: Shows a log of all changes to Git references.

34. Example: git reflog


git clean: Cleans untracked files from the working directory.

35. Example: git clean -f


git grep: Searches for a pattern in the contents of tracked files.

36. Example: git grep "function_name"


git clone --branch: Clones a specific branch of a remote repository to the local machine.

37. Example: git clone --branch feature-branch


https://github.com/user/repo.git
git branch --merged: Shows a list of branches that have been merged into the current
branch.

38. Example: git branch --merged


git cherry: Shows a list of commits that are not merged into another branch.

39. Example: git cherry main


git commit --amend: Modifies the most recent commit.

40. Example: git commit --amend -m "Updated feature"


git rebase --interactive: Allows for interactive rebasing.

41. Example: git rebase --interactive


git revert --no-commit: Reverts changes without committing them.

42. Example: git revert --no-commit abc123


git submodule: Manages Git submodules.

43. Example: git submodule init

Created by : Eng. Shady Ahmed +201023527158


git tag --list: Lists all tags in the repository.

44. Example: git tag --list


git config: Sets configuration options for Git.

45. Example: git config --global user.name "John Doe"


git blame -L: Shows who last modified a specific range of lines in a file.

46. Example: git blame -L 10,20 file.txt


git stash apply: Applies the most recent stash.

47. Example: git stash apply


git revert --abort: Aborts a revert that was in progress.

48. Example: git revert --abort


git bisect: Allows for a binary search for a specific commit.

49. Example: git bisect start


git add -p: Adds changes to the staging area interactively.

50. Example: git add -p


git merge --abort: Aborts a merge that was in progress.

51. Example: git merge --abort


git commit --allow-empty: Creates an empty commit.

52. Example: git commit --allow-empty -m "Empty commit"


git reset --hard: Resets the working directory and the repository to a previous commit.

53. Example: git reset --hard HEAD~1


git stash drop: Deletes the most recent stash.

54. Example: git stash drop


git log --pretty: Shows the commit history of the repository in a custom format.

55. Example: git log --pretty=format:"%h - %an, %ar : %s"


git rebase --continue: Continues a rebase that was in progress.

56. Example: git rebase --continue


git commit --no-verify: Commits changes without running pre-commit hooks.

57. Example: git commit --no-verify -m "Skip pre-commit hooks"


58. git bisect good: Marks a specific commit as good during a

Created by : Eng. Shady Ahmed +201023527158


git cherry-pick: Applies a specific commit from one branch to another.

59. Example: git cherry-pick abc123


git describe: Describes a specific commit relative to a tag or branch.

60. Example: git describe --tags


git revert --edit: Opens the default editor to modify the commit message when reverting
changes.

61. Example: git revert --edit abc123


git blame -w: Ignores whitespace changes when showing who last modified a line in a file.

62. Example: git blame -w file.txt


git commit --fixup: Creates a commit that is meant to be squashed with another commit.

63. Example: git commit --fixup abc123


git reset --soft: Resets the repository to a previous commit but keeps the changes in
the working directory.

64. Example: git reset --soft HEAD~1


git rebase --autosquash: Automatically marks commits created with --fixup or
--squash to be squashed during interactive rebase.

65. Example: git rebase --interactive --autosquash


git tag: Creates a tag in the repository.

66. Example: git tag v1.0.0


git blame --date: Shows who last modified a line in a file and when.

67. Example: git blame --date file.txt


git log --grep: Shows the commit history of the repository that matches a specific pattern.

68. Example: git log --grep="bugfix"


git cherry-pick --no-commit: Applies a specific commit without committing the changes.

69. Example: git cherry-pick --no-commit abc123


git reset --mixed: Resets the repository to a previous commit and clears the changes
from the staging area but keeps them in the working directory.

70. Example: git reset --mixed HEAD~1


git add -i: Adds changes to the staging area interactively with a menu.

71. Example: git add -i

Created by : Eng. Shady Ahmed +201023527158


git commit --squash: Creates a commit that is meant to be squashed with another commit
during interactive rebase.

72. Example: git commit --squash abc123


git rebase --skip: Skips applying a specific commit during interactive rebase.

73. Example: git rebase --skip abc123


git config --list: Lists all configuration options for Git.

74. Example: git config --list


git log --graph: Shows the commit history of the repository in a graph.

75. Example: git log --graph


git log --author: Shows the commit history of the repository by a specific author.

76. Example: git log --author="John Doe"


git bisect skip: Skips testing a specific commit during binary search with git bisect.

77. Example: git bisect skip


git merge --no-ff: Merges changes from one branch to another with a merge commit
even if it is a fast-forward merge.

78. Example: git merge --no-ff feature-branch


git merge-base: Finds the common ancestor between two branches.

79. Example: git merge-base branchA branchB


git rev-parse: Parses and displays the Git object id.

80. Example: git rev-parse HEAD


git add -e: Opens the default editor to modify the changes that will be added to the staging
area.

81. Example: git add -e


82. git branch -D: Deletes a branch even if it has unmerged changes.
Example: git branch -D feature-branch

git checkout -b: Creates a new branch and checks it out.

83. Example: git checkout -b new-branch


git commit --amend: Modifies the last commit with new changes or a new message.

84. Example: git commit --amend


git diff --cached: Shows the changes between the last commit and the staging area.

85. Example: git diff --cached

Created by : Eng. Shady Ahmed +201023527158


git fetch: Fetches changes from a remote repository.

86. Example: git fetch origin


git grep: Searches for specific text in the repository.

87. Example: git grep "search term"


git log --oneline: Shows the commit history of the repository in a concise format.

88. Example: git log --oneline


git push --tags: Pushes tags to a remote repository.

89. Example: git push --tags


git remote add: Adds a new remote repository.

90. Example: git remote add origin [email protected]:user/repo.git


git reset --hard: Resets the repository to a previous commit and removes all changes.

91. Example: git reset --hard HEAD~1


git show: Shows the changes of a specific commit.

92. Example: git show abc123


git stash: Stashes changes in the working directory.

93. Example: git stash


git submodule: Manages submodules in the repository.

94. Example: git submodule update


git tag -d: Deletes a tag.

95. Example: git tag -d v1.0.0


git blame --show-email: Shows the email address of the person who last modified a line
in a file.

96. Example: git blame --show-email file.txt


git cherry: Shows the commits that are in one branch but not in another.

97. Example: git cherry master feature-branch


git clone --depth: Clones a remote repository with a limited depth.

98. Example: git clone --depth=1 [email protected]:user/repo.git


git merge-base --is-ancestor: Checks if a commit is an ancestor of another commit.

99. Example: git merge-base --is-ancestor abc123 def456

Created by : Eng. Shady Ahmed +201023527158


git reset --merge: Resets the repository to a previous commit and clears the changes
from the staging area and working directory.

100. Example: git reset --merge HEAD~1

git rebase: Reapplies changes on top of another base tip.


101. Example: git rebase master
git revert: Creates a new commit that undoes the changes from a previous commit.
102. Example: git revert abc123
git show-branch: Shows the branch structure of the repository.
103. Example: git show-branch
git shortlog: Shows a summary of the commit history grouped by author.
104. Example: git shortlog
git submodule update: Updates submodules in the repository.
105. Example: git submodule update
git clean: Deletes untracked files from the working directory.
106. Example: git clean -f
git diff-tree: Compares the content and mode of blobs found via two tree objects.
107. Example: git diff-tree HEAD HEAD~1
git for-each-ref: Lists all references in the repository.
108. Example: git for-each-ref
git instaweb: Starts a local web server to browse the repository.
109. Example: git instaweb
git merge --no-ff: Forces a merge commit, even if it's a fast-forward merge.
110. Example: git merge --no-ff feature-branch
git pull --rebase: Fetches changes from a remote repository and rebases the current
branch.
111. Example: git pull --rebase origin master
git remote rename: Renames a remote repository.
112. Example: git remote rename origin upstream
git rerere: Records and replays resolutions of conflicted merges.
113. Example: git rerere
git tag -a: Creates an annotated tag with a message.
114. Example: git tag -a v1.0.0 -m "Release version 1.0.0"
git update-index: Registers files in the index and updates their state.
115. Example: git update-index --assume-unchanged file.txt
git whatchanged: Shows the commit history with the changes of each commit.
116. Example: git whatchanged
git blame: Shows who last modified each line of a file.
117. Example: git blame file.txt
git checkout --: Discards changes in the working directory for a specific file.
118. Example: git checkout -- file.txt
git clean: Deletes untracked files from the working directory.
119. Example: git clean -f
git count-objects: Displays statistics on object storage.
120. Example: git count-objects

Created by : Eng. Shady Ahmed +201023527158


git filter-branch: Rewrite branches to modify the commit history.
121. Example: git filter-branch
git gc: Cleans up unnecessary files and optimizes the local repository.
122. Example: git gc
git log --author: Shows the commit history of the repository filtered by author.
123. Example: git log --author="John Doe"
git mergetool: Runs a graphical merge tool to resolve conflicts.
124. Example: git mergetool
git pull --all: Fetches and merges changes from all remote repositories.
125. Example: git pull --all
git push --force-with-lease: Forces a push, but checks if the remote branch has
changed since the last pull.
126. Example: git push --force-with-lease origin master
git reflog: Shows the history of the branch references.
127. Example: git reflog

git remote show: Shows information about a remote repository.


128. Example: git remote show origin
git rev-parse: Shows the SHA-1 hash of a revision or object.
129. Example: git rev-parse HEAD
git stash apply: Applies the changes from the last stash to the working directory.
130. Example: git stash apply
git tag: Lists all tags in the repository.
131. Example: git tag
git blame --date: Shows who last modified each line of a file, along with the date.
132. Example: git blame --date file.txt
git checkout -b: Creates a new branch and checks it out.
133. Example: git checkout -b new-branch
git cherry-pick: Applies the changes from a specific commit to the current branch.
134. Example: git cherry-pick abc123
git describe: Describes the current or a specific commit with the closest annotated tag.
135. Example: git describe
git fetch: Fetches changes from a remote repository.
136. Example: git fetch origin
git log --grep: Shows the commit history of the repository filtered by a specific search
pattern.
137. Example: git log --grep="bug fix"
git merge-base: Finds the best common ancestor of two commits.
138. Example: git merge-base HEAD master
git push --set-upstream: Pushes the current branch to a remote repository and sets up
tracking.
139. Example: git push --set-upstream origin feature-branch
git remote prune: Deletes remote-tracking branches that are no longer on the remote
repository.
140. Example: git remote prune origin

Created by : Eng. Shady Ahmed +201023527158


git reset: Resets the index and working directory to a specific state.
141. Example: git reset HEAD~1
git stash: Stashes changes in the working directory for later use.
142. Example: git stash
git tag -d: Deletes a tag from the repository.
143. Example: git tag -d v1.0.0
git add -p: Interactively adds changes in the working directory to the index.
144. Example: git add -p file.txt
git bisect: Helps find the commit that introduced a bug using binary search.
145. Example: git bisect start; git bisect bad; git bisect good abc123
git clone: Clones a remote repository to the local machine.
146. Example: git clone [email protected]:username/repository.git

git fetch --tags: Fetches changes from a remote repository, including tags.
147. Example: git fetch --tags origin
git log --oneline: Shows the commit history of the repository in a condensed format.
148. Example: git log --oneline
git pull --rebase=preserve: Fetches changes from a remote repository and rebases the
current branch, preserving merges.
149. Example: git pull --rebase=preserve origin master
git rev-list: Lists the revisions in the repository that are reachable from a given commit.
150. Example: git rev-list HEAD

Created by : Eng. Shady Ahmed +201023527158

You might also like