2024-heraklion-git/cheatsheet.md
2024-08-26 14:50:17 +03:00

3.9 KiB

Git cheatsheet

Creating a repository

git init

Creates new git repository in current directory.

git clone <url> (<path>)

Clones the repository at the specified url. If no path is specified, the repository will be cloned into a directory with the same name as the remote repository.

Branches

git branch

Lists all branches in the repository.

git branch <branch-name>

Creates a new branch with the given name.

git switch <branch-name>

Switches to the specified branch.

git merge <branch-name>

Merges the specified branch into the current branch.

Making changes

git status

Shows the status of the repository. This includes the current branch and files that have been modified.

git diff (--staged)

Shows the changes that have been made to the files in the repository. Use --staged to see changes that have been added to the staging area.

git add <file>

Adds the specified file to the staging area.

git add .

Adds all files in the current directory to the staging area.

git reset (<file>)

Removes the specified file from the staging area. If no file is specified, all files are removed.

git commit (-m "<message>")

Commits all changes in the staging area to the current branch. If the -m flag is omitted, a text editor will open to write a commit message.

git commit --amend

Adds the staged changes to the last commit. This can be used for fixing typos in the commit message.

Undoing changes

git restore <file>

Restores the specified file to the state of the last commit. This undoes uncommitted changes.

git revert <commit>

Creates a new commit that undoes the changes of the specified commit. Use git log to find the hash of the commit.

git reset --hard <commit>

Resets the current branch to the specified commit. DANGER: This will remove all changes after the specified commit. Prefer git revert.

Looking at the history

git log (--oneline)

Shows all past commits on the current branch. Use --oneline to show a more compact view.

git show <commit>

Shows the changes of the specified commit. Use git log to find the has of the commit.

Remote repositories

git remote add <remote> <url>

Adds a new remote repository with the specified name (e.g. origin or upstream) and url. Origin is automatically created when cloning a repository.

git push <remote> <branch>

Pushes the specified branch to the remote repository.

git fetch <remote>

Fetches changes from the remote repository.

git pull <remote> <branch>

Fetches changes from the remote repository and merges them into the current branch.

Typical workflow

# 1. Fork the repository on GitHub / git.aspp.school

# 2. Clone the repository
git clone <fork-url>
git remote add upstream <upstream-url>

# 3. Create a new branch
git branch <topic-branch>
git switch <topic-branch>

# 4. Make changes to the code

# 5. Add and commit changes
git add .
git commit -m "<message>"

# 6. Push changes to your fork
git push origin <topic-branch>

# 7. Create a pull request on GitHub / git.aspp.school

# 8. Wait for the pull request to be reviewed and merged

# 9. Pull changes from the remote repository
git switch main
git pull upstream main

# 10. Delete the topic branch
git branch -d <topic-branch>

Whenever the remote repository is updated (i.e. when a pull request is merged), you need to pull the changes into your local repository.

git switch main
git pull upstream main

# If you have an active topic branch, you need to rebase it on top of the updated main branch
git switch <topic-branch>
git rebase main

Getting help

git help <command>

Shows the manual page for the specified command (add, commit, push, etc.).

Official Git documentation: https://git-scm.com/docs