DEV Community

Julia Shlykova
Julia Shlykova

Posted on

Cheat Sheet for Git

  • You have created a repository on github and want to work on the local machine:
git clone <repository_url HTTPS or SSH>
Enter fullscreen mode Exit fullscreen mode
  • Show the url that Git has stored:
git remote -v
Enter fullscreen mode Exit fullscreen mode

origin is the default name Git gives to the url that Git cloned from

  • Create a branch and switch to it:
git checkout -b <new_branch>
Enter fullscreen mode Exit fullscreen mode
  • Get the latest update from the main branch:
git checkout main
git pull origin main
git checkout <new_branch>
git merge main
Enter fullscreen mode Exit fullscreen mode

git pull = git fetch && git merge. git fetch updates local reference to the origin/main; git merge origin/main merges origin/main into main.

git pull --rebase = git fetch && git rebase.

  • push local branch to the remote and set the upstream
git push --set-upstream origin <new_branch>
Enter fullscreen mode Exit fullscreen mode
  • Check what upstream is assigned to the local branch:
git branch -vv
Enter fullscreen mode Exit fullscreen mode
  • Delete remote branch that is upstream:
git push origin :<new_branch>
Enter fullscreen mode Exit fullscreen mode

notice the colon!

  • Delete branch locally:
git checkout main
git branch -d <new_branch>
Enter fullscreen mode Exit fullscreen mode
  • Delete local references to deleted branches:
git remote prune origin
Enter fullscreen mode Exit fullscreen mode
  • Remove git commit which has not been pushed:
git reset HEAD~1
Enter fullscreen mode Exit fullscreen mode
  • Create local branch from an existing remote branch:
git checkout --track origin/<REMOTE_BRANCH_NAME>
Enter fullscreen mode Exit fullscreen mode

--track allows to set upstream and sets the same name.

  • Modify the last unpushed commit:
git commit --amend
Enter fullscreen mode Exit fullscreen mode

rebase vs merge

These commands integrate changes from one branch into another one in different ways.

git checkout feature
git merge main
Enter fullscreen mode Exit fullscreen mode

merge adds whole history of commits from main to feature.

git checkout feature
git rebase main
Enter fullscreen mode Exit fullscreen mode

This maves the starting commit of feature on the top of main last commit. If we check history of commits now, hashes of commits will be different from the original ones.

squash merge

We can squash all commits from the feature branch into 1 commit:

git checkout main
git merge --squash feature
git commit
Enter fullscreen mode Exit fullscreen mode

Interactive rebase

git checkout feature
git rebase -i main
Enter fullscreen mode Exit fullscreen mode

Since rebase creates new commits, we can change them to clean up a messsy history. It will open a list of the commits that are about to be moved:

pick 33d5b7a Message for commit #1
fixup 9480b3d Message for commit #2
pick 5c67e61 Message for commit #3
Enter fullscreen mode Exit fullscreen mode
  • fixup unites #2 commit with #1 commit with the message of #1 commit.

Cleanup

We don't necessarily need another branch to use interactive rebase:

git checkout feature
git rebase -i HEAD~3
Enter fullscreen mode Exit fullscreen mode

HEAD~3 indicates the last 3 commits.

Conflicts with rebase

In case of the conflicts. We have three options:

  1. git rebase --abort - undo the rebase. The branche's state returns to the state before git rebase was called.
  2. git rebase --skip - skip the commit. The changes made in the problematic commit won't be included.
  3. Resolve convlicts manually and run git rebase --continue

Important notes of rebase

  1. Much cleaner commit history. In case of git merge there is a commit about merging.
  2. Since rebase changes history of commits don't use it on public branches, that are used by other people.

Top comments (0)