https://docs.microsoft.com/en-us/windows/wsl/tutorials/wsl-git
Linux
sudo apt-get install git
Windows
https://git-scm.com/download/win
git config --global --list
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
Configure Git's default editor
# Set editor to nano
git config --global core.editor "nano -w"
# Set editor to vscode
git config --global core.editor "code --wait"
git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/libexec/git-core/git-credential-manager-core.exe"
--oneline
This is a shorthand for "--pretty=oneline --abbrev-commit" used together.
git log --oneline
Change previous commit
git commit --amend
View all branches
git branch
Create a branch
git branch <branch-name>
Delete a branch
# The branch must be fully merged in its upstream branch, or in HEAD if no upstream was set with --track or --set-upstream-to.
git branch -d <branch-name>
Rename a branch
# Must be on the branch first before renaming
git branch -m <new-name>
List remote tracking branches
git branch -r
Checkout a commit
git checkout <commit-hash>
Checkout a commit before HEAD
git checkout HEAD~n
# 1 commit before HEAD
git checkout HEAD~1
Discard changes and revert back to HEAD
git checkout HEAD <file>
# or
git checkout -- <file>
Clone a repo
git clone <url>
Show unstanged changes
git diff [file]
Show staged and unstanged changes since head
git diff HEAD [file]
Show staged changes
git diff --staged [file]
Show changes between branches
git diff branch1..branch2
Show changes between commits
git diff commit1..commit2
Fetch changes from remote repo, but do not merge changes into current HEAD branch.
git fetch <remote>
# if not specified, <remote> defaults to origin.
Merge branch
git merge <branch-name>
- Open and edit the conflicted file.
- Stage the new merged content by executing
git add <file>
. - Finalize the merge by executing
git commit -m "merge and resolve conflict in <file>"
.
Fetch changes from remote repo and merge changes into current branch.
git pull <remote> <branch>
Push to repo
git push <remote> <branch>
Undo commit
git reset <commit-hash>
Undo commit and file changes
git reset --hard <commit>
Discard changes and revert back to HEAD
git restore <file>
# equivalent
git checkout HEAD <file>
Unstage file
git restore --staged <file>
Undo a commit by creating a new commit
git revert <commit-hash>
Show existing remote repo
git remote
# verbose
git remote -v
Add a new remote
git remote add <name> <url>
Rename remote
git remote rename <old> <new>
Remove remote
git remote remove <name>
Save changes to stash
git stash
Remove most recently stashed changes
git stash pop
List stash
git stash list
Apply most recent stash without removing it
git stash apply
Apply stash by index
git stash apply stash@{n}
Drop a stash by index
git stash drop stash@{n}
Switch branch
git switch <branch-name>
Create and switch branch
git switch -c <branch-name>
Switch to previous branch that you were on
git switch -
error: update_ref failed for ref 'refs/remotes/origin/master': cannot update the ref 'refs/remotes/origin/master': unable to append to '.git/logs/refs/remotes/origin/master': Permission denied
Solution:
sudo chown -Rc $UID .git/