Git - alexanderteplov/computer-science GitHub Wiki

Git

Description

Git is a centralized version control system. It's scalable, performant, network low-dependent, space-efficient, reliable.

Generally you have two version one the same repository: local and remote. It looks as .git folder in your project root folder.

You may also often want to specify .gitignore file at the same project root level to describe files you do want to track with git (either by filename or by mask).

Git stores your project files in an indirect way: as incremental changes between current and the previous state, starting from the empty state.

The most common Git commands you should know

  • git init
  • git clone
  • git add
  • git stage
  • git commit
  • git cherry-pick
  • git fetch
  • git pull
  • git push
  • git branch
  • git checkout
  • git stash
  • git rebase
  • git merge
  • git status
  • git diff
  • git reflog
  • git reset
  • git blame

Rebase vs Merge

Merging is nice because it’s a non-destructive operation. The existing branches are not changed in any way.

On the other hand, this also means that the feature branch will have an extraneous merge commit every time you need to incorporate upstream changes.

The major benefit of rebasing is that you get a much cleaner project history. First, it eliminates the unnecessary merge commits required by git merge. Second, as you can see in the above diagram, rebasing also results in a perfectly linear project history.

But, there are two trade-offs for this pristine commit history: safety and traceability. If you don’t follow the Golden Rule of Rebasing, re-writing project history can be potentially catastrophic for your collaboration workflow. And, less importantly, rebasing loses the context provided by a merge commit.

Always use interactive rebasing git rebase -i main.

The Golden Rule of Rebasing

The golden rule of git rebase is to never use it on public branches.

Force-Pushing

If you try to push the rebased main branch back to a remote repository, Git will prevent you from doing so because it conflicts with the remote main branch git push --force.

Hooks

The most common githooks:

  • pre-commit
  • pre-merge-commit
  • post-commit
  • post-merge
  • pre-push

Links

⚠️ **GitHub.com Fallback** ⚠️