User Tools

Site Tools


git_cheatsheet

Git Cheatsheet

  • git init: Initializes a new Git repository.
  • git clone [url]: Creates a local copy of a remote repository.
  • git status: Displays the state of the working directory and staging area.
  • git add [file]: Adds a file to the staging area.
  • git reset [file]: Unstages a file while retaining the changes.
  • git diff –staged: Shows differences between the staging area and the last commit.
  • git commit -m “[message]”: Records staged changes with a descriptive message.
  • git branch: Lists all local branches.
  • git checkout -b [name]: Creates and switches to a new branch.
  • git log: Displays commit history.
  • git remote add [ref] [url]: Adds a new remote repository.
  • git push [alias] [branch]: Uploads local branch commits to a remote repository.
  • git pull: Fetches and merges changes from the remote to the local repository.
  • git stash: Temporarily stores modified tracked files.
  • git stash pop: Restores the most recently stashed files.
  • git stash drop: Discards the most recently stashed changeset.
  • git rebase [branch]: Reapplies commits on top of another base tip.
  • git rebase -i HEAD~<n>: Starts an interactive rebase for the last n commits.
  • git reset –hard [commit]: Resets the working directory to a specified commit.
  • git log branchB..branchA: Shows commits on branchA that are not on branchB.
  • git diff branchB…branchA: Displays differences between two branches.
  • git show [SHA]: Shows the changes in a specific commit.
  • 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

git_cheatsheet.txt · Last modified: by smickster

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki