Skip to main content

Git Commands

Cheatsheet

CommandDescription
git initInitializes a new Git repository
git clone <repository>Clones a repository into a new directory
git add <file>Adds file changes to the staging area
git commit -m "message"Records changes to the repository with a commit message
git statusDisplays the status of changes
git diffShows changes between commits, commit and working tree, etc.
git branchLists all local branches or creates a new branch
git checkout <branch>Switches to a different branch
git merge <branch>Merges changes from a specified branch into the current branch
git pullFetches and merges changes from a remote repository
git pushPushes local changes to a remote repository
git remote remove originRemoves remote origin
git remote add <name> <url>Adds a remote repository
git remote remove <name>Removes a remote repository
git logDisplays commit history
git reset <file>Resets the file's changes in the staging area
git revert <commit>Reverts a specific commit
git stashTemporarily saves changes that are not ready to be committed
git tag <tag_name>Adds a tag to a specific commit
git fetch --pruneFetches changes and removes references to deleted branches

git workflow

WATCH VIDEO HERE:

Roll back to previous commit or push

1 ) Get all actions you've done

Git log —oneline

2) Figure out which one you want to go back to

Git checkout (the number)
Git reset (the number) removes everything else after it

To Note! : Resetting doesn’t delete extra files

Git reset —hard (the number) https://stackoverflow.com/questions/9529078/how-do-i-use-git-reset-hard-head-to-revert-to-a-previous-commit

To force dev branch to become main branch (protected branch)

this could be due to the fact that there are many merge conflicts between dev and main branch.

  1. Create backup branch for main (main-backup)
  2. Kill the main branch
  3. Create new branch main using dev
  4. git switch main
  5. unprotect main on gitlab/github
  6. force push as main
    git push origin main -f