Git concepts
Concepts are more important than commands. I don’t try to memorize all commands, I prefer making aliases and learning how Git works.
3 Trees
- the working directory: the files and directories you modify
- the index: the changes you stage with a
git add
for the nextgit commit
- the HEAD: a reference that points to the last commit of the current branch and moves when you switch to another branch
Local vs. Remote
The local repository is your machine. You use the remote repository to host your project, for example, on GitHub, GitLab, Bitbucket, etc. Each member of the team work on his local repository and then pushes the modifications to the remote repository when it's done.
Clone, status, pull and push
-
git clone
is the first command you learn. It copies a remote repository wherever you want if you have access. -
git status
allows you to know the current status. Use it as much as you like. -
git pull
incorporates changes from a remote repository into the current branch whereasgit push
updates remote refs using local refs.
Branches
The default branch is main or master. You can create new branches to safely diverge from the main branch. When you have finished, you can merge your new branch in the main branch.
Tags
Tags are versions for a particular branch at a moment in time. Fork It's the act of copying a repository to use it for your own purpose, safely experiment with it or contributing.
Merging vs. rebasing
It's the same purpose: incorporating modifications from one side to another. Merging is safer as it remembers the whole history but it's verbose. Rebasing is cleaner for the history but a bit more risky because it flattens changes as a linear series.
Advanced commands
Use command lines for relatively complex operations. I strongly recommend you use them as aliases. Don’t waste your time memorizing:
Amending a bad commit (BEFORE git push
)
git add {file} && git commit --amend
and if you don't need to modify the commit message, you can do git add {file} && git commit --amend --no-edit
.
Stage and unstage only some modifications in a file
git add -p {file}
and git reset -p {file}
Grab modifications from another branch without merging or rebasing
git cherry-pick {commit_sha}
Unstage all modifications in one command (~ unadd)
git reset HEAD
Change remote destination
Don't do git remote rm origin && git remote add origin {URL}
. Instead run git remote set-url origin {URL}
.
List ignored files
git ls-files --others -i --exclude-standard
Merge all modifications but squash them in one commit
git merge --squash {branch} && git commit
GUI clients
Be efficient, don’t waste your energy typing the same command over and over. GUI clients make sense for simple operations such as cloning, pulling/pushing modifications, and creating new branches.
Besides, it’s not uncommon to get conflicts when merging branches, and you should not handle conflicts without a graphical interface.
GitKraken
This client is simply beautiful. Download latest version of GitKraken
SmartGit
This client is efficient. Download latest version of SmartGit
Submodules
Submodules allow for including other git repositories in your repository as references. When you add submodules, it creates a config file called .gitmodules
at the root of your project.
Adding a submodule
git submodule add {submodule_address} && git add {submodule_folder_path} && git commit
Cloning a git project with submodules
Nothing to do. The git clone
command already grabs them, but don't forget git submodule update --init --recursive
the first time to initialize all repositories locally.
How to update submodules
You can run git submodule update --remote --recursive
How to delete a submodule
Don't do it manually! Instead, run: git submodule deinit {submodule} && git rm {submodule_folder_path} && git commit
Configurations
Git works with a cascade of configuration files. You can define global configurations and override them locally for a particular project or a group of folders.
Global configuration
You can either edit ~/.gitconfig or run: git config --global --edit
. You can also define specific git config per each project and folder. It will override the global config. Global list of ignored files and directories You can edit ~/.gitignore_global. You can also define specific .gitignore per each project and folder. It will override the global gitignore.
Git aliases
Aliases are custom shortcuts for git commands. You can edit ~/.gitconfig or run: git config --global --edit
, or even run something similar to git config --global alias.st status
.
Use ohmyzsh and its git plugins
You can add the git plugin to your config. You'll get excellent shortcuts and aliases.
Top comments (3)
This was really helpful and concise. Thank you!
pleasure!
WoW great Job 👍