Skip to content

Instantly share code, notes, and snippets.

@dukenmarga
Last active January 19, 2024 14:49
Show Gist options
  • Save dukenmarga/6525566612f7943ad83010640d38dab6 to your computer and use it in GitHub Desktop.
Save dukenmarga/6525566612f7943ad83010640d38dab6 to your computer and use it in GitHub Desktop.
Git Cheat Sheet

Initialise a repository

git init

Basic

# Add
git add somefile.py
git add .
git add ../somefile.go

# Reset change to previous committed state (changes will be gone, be careful!)
git checkout file.py

# Check difference
git diff somefile.py

# Check difference on file that has been in staging
git diff somefile.py --staged

Push to remote

git push origin main --verbose

Merge

git checkout main
git merge --no-ff features # this will merge branch features into main

Undo last commit, but keep changes (not committed to remote)

git reset HEAD~1

Delete branch

Local

git checkout main
git branch -d local_branch

Remote

git push origin --delete remote_branch

Git Flow

If there is branch divergent between local and remote:

$ git flow feature finish refactor-predictor
Branches 'feature/refactor-predictor' and 'origin/feature/refactor-predictor' have diverged.
And local branch 'feature/refactor-predictor' is ahead of 'origin/feature/refactor-predictor'.

It means we need to resolve both branch by pulling from the remote and push it to the remote again

# Update local feature/refactor-predictor branch
git pull origin feature/refactor-predictor

# Push changes to origin
git push origin feature/refactor-predictor

If there is branch divergent, but the local may be fast-forwarded, then to resolve this we can add additional step from above

Branches 'develop' and 'origin/develop' have diverged.
Fatal: And branch 'develop' may be fast-forwarded.
# Update local develop branch
git pull origin develop

# Push changes to origin
git push origin develop

# Fast-forward develop if needed
git checkout develop
git pull origin develop --ff-only
git push origin develop

Branch

Deleted a branch but it is still showing up when you use the tab completion

git fetch --prune

This command will update your local Git repository with the latest changes from the remote repository and also remove any references to deleted branches

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment