Skip to content

Instantly share code, notes, and snippets.

@ziritrion
Last active May 26, 2024 11:36
Show Gist options
  • Save ziritrion/d73ca65bf4d19c79ca842a55853cb962 to your computer and use it in GitHub Desktop.
Save ziritrion/d73ca65bf4d19c79ca842a55853cb962 to your computer and use it in GitHub Desktop.
git cheatsheet

Basic git

  1. Make sure your local copy of the selected branch is updated.
    1. Without overwriting anything
      • git fetch
    2. If you already fetched or you are ready to overwrite your local copy, then pull
      • git pull
  2. Check your repo branches
    1. Local branches
      • git branch
    2. All branches on remote repo
      • git branch -r
    3. Both local and remote branches
      • git branch -a
    4. You can also add -v to make the commands explicitly verbose
  3. Create a branch and access it
    1. Normal way
      1. git branch new_branch
      2. (2 ways)
        • git checkout new_branch
        • git switch new_branch > Recommended option (avoid checkout unless necessary)
    2. Shortcut (2 ways)
      • git checkout -b new_branch
      • git switch -c new_branch > Recommended option (avoid checkout unless necessary)
  4. Get some work done lol
  5. Check the status of your work
    • git status
  6. Did you mess up editing a file and want to restore it to how it was beforehand?
    • git restore changed_file.txt
  7. Add changes to staging in order to prepare your commit
    1. Add a single file
      • git add new_file.txt
    2. Add all changed files
      • git add . -p
  8. Did you screw up? Reset the staging
    • git reset
  9. Commit
    • git commit -m "This is a commit message"
  10. Check the commit history of the branch you're in
    • git log
    • If you wanna see some cool things with log, you can use something like this:
      • git log --graph --oneline --all
  11. Make sure you upload your commits to the remote repo! If your local branch is brand new, you must add it to the remote repo.
    1. New branch
      • git push -u origin new_branch
    2. Previously existing branch
      • git push
  12. Move to another branch
    • git checkout another_branch
  13. Merge some branch into your current branch (assuming default behavior of pull is merge)
    • git pull branch_that_will_be_merged_into_current_branch

For more info check the GitHub Git Cheat Sheet

Checkout vs Switch

checkout can be used to switch branches and/or restore working tree files, which means that you can do things like undo/restore commmits and overwrite local changes, or detach the HEAD (navigating a commit which is not the latest on its branch).

switch is only used for switching and creating branches. It cannot discard changes to tracked files: if you've changed a tracked file and want to switch branches, you'll need to stash or commit the changes.

Advanced git

The following are some best practices that may be useful, taken from this blog post, as well as this tip.

  1. While working on a branch, if you need to pull commits from the remote repo to your local repo, use rebase instead of merge to reduce the amount of commits
    • git pull --rebase
    • If you want to make rebasing the default behavior when doing git pull, do so with git config --global --bool pull.rebase true
  2. Before pushing your changes to the remote repo, perform basic housekeeping (squash related commits together, rewording messages, etc)
    • git rebase -i @{u}
  3. Make sure that you've fetched all changes from the remote repo
    • git fetch
  4. Simulate a merge to see any possible conflicts:
    1. Do a merge with the --no-commit flag from the work branch.
      • get merge --no-commit --no-ff $GOOD_BRANCH
    2. Examine the staged changes
      • git diff --cached
    3. Undo the merge
      • git merge --abort
  5. Merge (do not rebase) changes from master/main into your branch, in order to update the branch with the latest features and solve any compatibility issues and/or conflicts
    1. git merge main
    2. git pull --merge main
  6. Enforce merge commit when merging feature branch into main, even if a merge commit isn't necessary (check next point for exception), in order to make it easier to see the where and when of changes. Assuming you're in main:
    • git merge --no-ff branch_that_will_be_merged_into_main
  7. Exception to point 4: if you only need to merge a single commit (typical for stuff such as bugfixes). Assuming you're in main:
    • git cherry-pick branch_that_only_has_a_single_commit
  8. Delete merged branch:
    1. Delete locally
      • git branch -d branch_that_has_been_merged
    2. Delete on remote repo
      • git push origin :branch_that_has_been_merged

Create a remote repo (local folder as remote repo)

Official method

Source

  1. Make sure you've got a local commit. You may initialize a local repo with git init on any project folder and making sure that it has at least one commit, or you may use an already existing local repo.
  2. On a separate folder, run:
    git clone --bare path/to/local/project project.git
    • This will create a folder with name project.git on the folder you're running the command.
    • Remote repo folders use the .git extension as a standard.
    • This folder is a bare repository. It does not contain a working folder, only the git files.
  3. Move the project.git folder to the final destination. Ideally, a shared folder such as a networked drive that everyone has access to "locally".
    • You may combine steps 2 and 3 by creating the bare repo directly on the final folder.
  4. You should now be able to clone the repo:
    git clone path/to/remote/repo/project.git
  5. The original repo that we bare-cloned does not have an origin repo to push to. If you want to keep using it, set up a remote like this:
    git remote add origin path/to/remote/repo/project.git

Alternative method

Source

  1. On remote folder:
    mkdir my_repo
    cd my_repo
    git init --bare
  2. On local folder:
    cd my_repo
    git init
    git remote add origin ssh://myserver/my_repo
    git add .
    git commit -m "Initial commit"
    git push -u origin master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment