Skip to content

Instantly share code, notes, and snippets.

@HateFindingNames
Created April 7, 2024 11:02
Show Gist options
  • Select an option

  • Save HateFindingNames/a537802f3707347c31eb29ea1eb042f6 to your computer and use it in GitHub Desktop.

Select an option

Save HateFindingNames/a537802f3707347c31eb29ea1eb042f6 to your computer and use it in GitHub Desktop.
Git/Github cheat sheet

Workflow new Repository

cd to project dir.

Note

Optional: create a file .gitignore and add all file endings, relative paths and/or file names not to be included to the repository.

Then

  • git init Initializes the repo with branch main.
    • Optional: git init -b <branch name> Initializes the repository with a custom branch name.
  • git add . Add (stage) everything not excluded by .gitignore.
  • git commit -m '<commit message>' Commit all staged changes inlcuding a commit message.
  • git remote add origin <url> Add remote location like Github, Gitlab or whatev.
  • git push origin main Push branch main to origin.

Tip

To stages all changed and tracked files excluding untracked files and commit them all with a single command use the git commit option -a | --all like git commit -a -m <commit message>.

Branching

Create a new branch

git branch -b <branch name>

Switch to the new branch

git checkout <branch name>

Delete a specific branch

git branch -D <branch name>

Undoing changes

Reset to the latest commit

This resets the project to the state of the latest commit inlcuding local files and the commit history itself.

git reset --hard HEAD

Reset to a specific commit

git reset --hard <commit hash>

Get commit hash to revert to using git log --oneline.

Useful commands

Staging specific files

git add <file name or directory>

Checking current status

git status

Display commit history

git log
git log --oneline # output single line style
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment