This is an old revision of the document!
Git Cheatsheet
0. git init: Initializes a new Git repository. 1. git clone [url]: Creates a local copy of a remote repository. 2. git status: Displays the state of the working directory and staging area. 3. git add [file]: Adds a file to the staging area. 4. git reset [file]: Unstages a file while retaining the changes. 5. git diff –staged: Shows differences between the staging area and the last commit. 6. git commit -m “[message]”: Records staged changes with a descriptive message. 7. git branch: Lists all local branches. 8. git checkout -b [name]: Creates and switches to a new branch. 9. git log: Displays commit history. 10. git remote add [ref] [url]: Adds a new remote repository. 11. git push [alias] [branch]: Uploads local branch commits to a remote repository. 12. git pull: Fetches and merges changes from the remote to the local repository. 13. git stash: Temporarily stores modified tracked files. 14. git stash pop: Restores the most recently stashed files. 15. git stash drop: Discards the most recently stashed changeset. 16. git rebase [branch]: Reapplies commits on top of another base tip. 17. git rebase -i HEAD~<n>: Starts an interactive rebase for the last n commits. 18. git reset –hard [commit]: Resets the working directory to a specified commit. 19. git log branchB..branchA: Shows commits on branchA that are not on branchB. 20. git diff branchB…branchA: Displays differences between two branches. 21. git show [SHA]: Shows the changes in a specific commit. 22. git config –global core.excludesfile [file]: Sets up a global file for ignoring files.
Use oh-my-zsh and you only have to type 2-3 letters for each command
Here are a few important Git commands that might have been missed:
1. git init –bare: Initializes a bare repository, useful for central repositories. 2. git remote -v: Lists all remotes with their fetch and push URLs. 3. git log –oneline: Displays a compact, one-line view of the commit history. 4. git cherry-pick [commit]: Applies a specific commit to the current branch. 5. git revert [commit]: Reverts changes from a specific commit by creating a new commit. 6. git mv [file] [newfile]: Renames or moves a file in the repository. 7. git tag [name]: Adds a tag to a specific commit for versioning. 8. git clean -f: Removes untracked files from the working directory. 9. git blame [file]: Shows who modified each line in a file. 10. git bisect: Helps find a bug by performing a binary search between good and bad commits.
git stash list shows all the stashed items git stash show -p stash@{x} —name-only files changed in a particular stash git stash pop stash@{x} pop’s stash specified git checkout stash@{x} —<filename> pop’s a particular file from the mentioned stash
I use GitExtensions application, so, I dont have to remember any commands.
Got to include the -p flag for add and checkout at least… Add or revert changes in working copy using an interactive patch viewer. I use add -p nearly every time I prepare a commit.
It's missing: git stash apply which applies the stash but preserving the stash without dropping it
git cat-file -p <hashedcommit> . It is very useful to gather information about a particular commit.
It looks perfect, and thanks for sharing! If I were to add something, I’d suggest an important and useful alias to show the log cleanly and visually:
git config –global alias.clog 'log –oneline –graph'
`git switch [name]` to change branch `git switch -c [new-name]` to create and change to branch are nicer than checkout
To quickly switch back to the previous branch: git checkout -
This is a great cheat sheet for essential git commands! One thing I would add is the git fetch“ command, which downloads changes from a remote repository but does not merge them into the local branch.
git branch -m <newbranch> to rename the name of branch before publish. git stash -u is the one that I almost use daily. I use more git init,status,add and good and old push -f kk Perhaps add: git blame, git bisect Git squash? Instructif
