Skip to content

Instantly share code, notes, and snippets.

@mvndaai
Last active December 16, 2023 01:59
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvndaai/86dab188665d2f8ac4eff9ab16c48199 to your computer and use it in GitHub Desktop.
Save mvndaai/86dab188665d2f8ac4eff9ab16c48199 to your computer and use it in GitHub Desktop.

git

This is list of the git commands I use.

The usual flow of developed is clone or fork a repo, create a branch, change code, create commits at logical stopping points, push those commits, and finally merge the branch (usually done on a website).

Branches

Branches let you create a copy all the code from whatever branch you are on. This lets you make changes that can be merged back into the original branch, but doesn't effect the original. They help with conflicting changes by multiple people and code reviews.

Create new branch

git checkout main
git pull
git checkout -b <branch name>

Push branch

git push origin HEAD

Note: If your commit is amended or you deleted a commit you can force the changes

git push origin HEAD --force

Merge main branch changes then push

git checkout main
git pull
git checkout -
git rebase main
git push origin HEAD

Delete local branch

git branch -d <branchName>

Delete all merged local branches

git branch --merged | egrep -v "(^\*|master|main)" | xargs git branch -d

Commits

Commits let you save current changes in a name on your local computer. Commits can later be pushed to a repo.

Add files and commit with message

git commit -am "<commit message>"

Ammend commit

git commit --amend
git commit --amend --no-edit

Undo commit

git reset --soft HEAD~

Other

Pull main and keep current changes

# If you are on main
git add .
git commit -m tmp
git pull --rebase
git reset --soft HEAD~
# If you are on another branch
git commit -am tmp
git fetch
git rebase origin/main
git reset --soft HEAD~

Pull from remote branch without upstream

git pull origin `git branch --show-current`

Switch to last checked out branch

git checkout -

Setup fork to be rebased with the original repo

git remote add upstream <repo location>

Update fork with changes from original main

git fetch upstream
git rebase upstream/main
git push origin HEAD

Copy a commit from another branch

Find a commit hash. It is on the left in this command:

 git log --abbrev-commit --pretty=oneline

Cherry pick the commit

git cherry-pick <commit hash>

Ignore local changes of a config file

git update-index --skip-worktree <file path>
# Undo
git update-index --no-skip-worktree <file path>

This might work better in some situations

git update-index --assume-unchanged <file path>
# Undo
git update-index --no-assume-unchanged <file path>

Prettier looking through history

The normal way to look through history

git log

To make that eaiser to follow create an alias then use it. Copied from here.

git config --global alias.logline "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold yellow)<%an>%Creset' --abbrev-commit"
git logline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment