Git cheat sheet

2 min read

git_logo

Some definitions

Git is a version-control tool for tracking changes in source code during software development.

A commit records a snapshot of your changes and associate it with a message and a commit id.

A branch is a separate version of the main repository that allows the developper to test a new feature for example without affecting the main repository.

Staging means adding a file to the Git index before commiting it.

Remote repositories are versions of your source code that are hosted on the Internet, mainly to share the code or collaborate with others. Examples of Git-based source code repository hosting service include Github, Bitbucket or Gitlab.

You can download a printable Git command line cheat sheet here (Source).

git workflow Source

Create a repository

  • create a new local repository
git init my_project_name
  • Download from an existing repository
git clone my_url

Observe the repository

  • list files not yet commited
git status
  • Show full change history
git log
  • Show change history for file/directory including diffs
git log -p [file/directory]
  • Show the changes to files not yet staged
git diff
  • Show the changes between two commit ids
git diff commit1 commit2

Working with branches

  • List all local branches
git branch
  • Create a new branch
git branch new_branch
  • Switch to a branch and update working directory
git checkout my_branch
  • Create a new branch and switch to this branch
git checkout -b new_branch
  • Delete a branch
git branch -d my_branch
  • Merge branch_a into branch_b
git checkout branch_b
git merge branch_a

Resolve merging conflicts

git mergetool --tool=emerge

It will open 3 windows: version a on top left, version b on top right and final version at the bottom.

  • press n for next change
  • press a or b to choose which version I want to keep
  • press q to quit and save

Make a change

  • Stage all changed files in current directory
git add .
  • Commit all your tracked files to versioned history
git commit -am "commit message"
  • Unstages file, keeping the file changes
git reset [file]
  • Revert everything to the last commit
git reset --hard

Synchronize with remote repository

  • Get the latest changes from origin (no merge)
git fetch
  • Fetch the latest changes from origin and merge
git pull
  • Push local changes to the origin
git push

Add an existing SSH key to the agent

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

Fix remote repo not empty

When I want to push to a remote repository that is not empty (for example if it was initialised with a license or readme file).

git fetch origin main:tmp
git rebase tmp
git push origin HEAD:main
git branch -D tmp

Leave a comment