Git Cheat Sheet
Setting up repository
Setup
git config --global user.name "{firstname lastname}"
Set the author name to be used for all commits by the current usergit config --global user.email "{email}"
Set the email to be used for all commits by the current usergit config --global --edit
Open the global configuration file in a text editor for manual editing
Initializing
git init
Creates a new git repository in the directory
Clone
git clone {url}
Clone the repogit clone {url} {directory}
Clone repo into the directory
Changes
Adding
git add {file_name}
Add the file to staginggit add .
Add all file to stagingit add *{extension}
Add all extensions (like .vue
) to staginggit add {directory}
Stages the changes in all files in directory
Removing
git rm {file_name}
Removes the file from both git and file systemgit rm --cached {file_name}
Only remove file from git index
Reseting
git reset {file_name}
Remove file_name file from staged area.git reset
Reset staging area to match most recent commit, but leave the working directory unchangedgit reset --hard
Same as previous and overwrites all changes in the working directorygit reset {commit}
Move the current branch tip backward to commit, reset the staging area to match, but leave the working directory unchangedgit reset --hard {commit}
Same as previous, but resets both the staging area & working directory to match. Deletes uncommitted changes, and all commits after commit
Diff
git diff
View the changes made but not yet staged
Commiting
git commit -m "{message}"
Commit the staged snapshotgit commit --amend
Combine staged changes with the previous commit, or edit the previous commit message without changing the snapshotgit commit --amend --no-edit
Amend a commit without changing it's message
Pushing
git push {my-remote} {my-branch}
Pushes the commit to my-remote my-branchgit push -f
Force push to remote branch with local branch.git push --force-with-lease
is a safer option that will not overwrite any work on the remote branch if more commits were added to the remote branch
Branching
Show
git branch
Shows all branch and current branch is shown with a asteriskgit branch -a
Shows all the branches including local and remote
Creating and Deleting
git branch {branch}
Creates a branchgit branch -d {branch}
Deletes a branchgit push origin --delete {branch}
Delete a remote branch
Switch branch
git switch {branch}
or git checkout {branch}
Switch to branchgit switch -c {branch} {start_point}
or git checkout -b {branch} {start_point}
Create a new branch and switch to itgit switch -C {branch} {start_point}
or git checkout -B {branch} {start_point}
Create a new branch and switch to it, even if the branch already exists.If the branch already exists, this option will overwrite it with the current HEAD commit
Switch to a specific commit without changing branch
git switch --detach {commit}
or git checkout [--detach] {commit}
Switch to a specific commit without creating a branch
Detached head
git switch --detach {branch}
Switch to the latest commit of a specific branch. This is also known as being in "detached HEAD" state
Log
git log
Shows the log of commitgit log --oneline --graph --decorate
Shows log of commits, each commit with single line with a graphgit log --oneline --graph --decorate --all -n 30
Shows 30 log with all branches in the repo with graphgit log -p {file_name}
Changes over time for a specific file
Rebasing
git rebase {branch}
Rebase the current branch onto the specified branchgit rebase --continue
If there is any conflicts during rebase, git will pause and after resolving them use this command to continuegit rebase --abort
Abort the rebase process and return to the original stategit rebase --skip
To skip a specific commit during the rebase processgit rebase -i {commit_id}
Rebase commits from a commit
Stashing
git stash
Stashes the staged and unstaged changes.git stash -u
Stashes everythings including unstashed filegit stash list
List of all stashesgit stash pop
Delete the recent stash and apply itgit stash pop stash@{2}
Delete the {2} stash and apply itgit stash show
Shows the description of stashgit stash apply
Keep the stash and apply itgit stash drop
Delete all stash