Skip to content

Instantly share code, notes, and snippets.

@Mirakurun
Last active February 18, 2022 19:05
Show Gist options
  • Save Mirakurun/19390a371acc91d98fa86cdd45db0c0a to your computer and use it in GitHub Desktop.
Save Mirakurun/19390a371acc91d98fa86cdd45db0c0a to your computer and use it in GitHub Desktop.
Git Notes

Git Notes

https://docs.microsoft.com/en-us/windows/wsl/tutorials/wsl-git

Installing Git

Linux

sudo apt-get install git

Windows

https://git-scm.com/download/win

Git Config List

git config --global --list

Git Config File Setup

git config --global user.name "Your Name"
git config --global user.email "youremail@domain.com"

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 Credential Manager Setup

git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/libexec/git-core/git-credential-manager-core.exe"

Git Log

--oneline

This is a shorthand for "--pretty=oneline --abbrev-commit" used together.

git log --oneline

Git Amend

Change previous commit

git commit --amend

Git Branch

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

Git Checkout

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>

Git Clone

Clone a repo

git clone <url>

Git Diff

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

Git Fetch

Fetch changes from remote repo, but do not merge changes into current HEAD branch.

git fetch <remote>

# if not specified, <remote> defaults to origin.

Git Merge

Merge branch

git merge <branch-name>

Resolving Merge Conflicts

  1. Open and edit the conflicted file.
  2. Stage the new merged content by executing git add <file>.
  3. Finalize the merge by executing git commit -m "merge and resolve conflict in <file>".

Git Pull

Fetch changes from remote repo and merge changes into current branch.

git pull <remote> <branch>

Git Push

Push to repo

git push <remote> <branch>

Git Reset

Undo commit

git reset <commit-hash>

Undo commit and file changes

git reset --hard <commit>

Git Restore

Discard changes and revert back to HEAD

git restore <file>

# equivalent
git checkout HEAD <file>

Unstage file

git restore --staged <file>

Git Revert

Undo a commit by creating a new commit

git revert <commit-hash>

Git Remote

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>

Git Stash

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}

Git Switch

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 -

Creating a personal access token for GitHub

https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token

Git Errors

After using sudo git push

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/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment