Git is a common version control used on Software Engineering projects. You could see that every projects maintained online used git in majority. Learning git is a necessity if you would like to work in a codebase maintained by a group or company. This post currates a list of common knowledge and guide I found and followed.
A codebase is stored in a repository either local or on a remote server. In the repository, there is a “main” branch that represents the current progress or release. The repositories often have many branches of others. You save your progress by creating a commit consists of your changes into a branch. You could branch from other branch and merge a branch into other branch.
With git, you could work on a task alongside with others’ work in the same codebase. This is important as you need to store your own work and merge with the rest of others’ work in a good way.
There are sites that host remote repositories such as github, gitlab, etc. These sites have utility features such as continous integration/deployment (CI/CD) and merge request that can be reviewed by others.
This is a list of git commands I have used.
This command is used to initialize a local repository. The .git
folder is initialized by this command.
This clones a remote repository into a folder. An origin remote is set to able to pull and push to the remote repository.
This command is used to manage remote repositories it relates to.
This command stages files to be commit.
This commits changes staged using git add
. You also could modify previous commit using -a
.
This command pull commits from remote repository. If executed on different branch, it will create a merge commit to apply pulled commits.
This command pushes commits created locally to the remote repository. This creates new branch if it doesn’t exist.
You could use --force
to rewrite commit history with your local commit history.
This command outputs an overview of your local repository. This includes current branch and uncommited changes if any.
This command creates a new branch for current branch. Keep in mind that you need to select the new branch using checkout.
This command changes the current branch to specified branch.
You could use -b
to create new branch and checkout to the new branch.
git log
is used to see the history of commits on current branch.
You could see the code on that commit version using git checkout <commit_sha>
.
git reflog
is used to see the history of git command you used.
This command stashes your uncomitted changes. Useful when you need to pull or change branch.
To unstash, use git stash apply
.
This adds commits added from source branch before repeating your commits on current branch.
These are tips and tricks that could be used for convenience.
Before merging your work into the main remote branch, you often need to update your branch to the latest commit done in main branch.
You could just git pull origin master
to update your branch, but this will create a merge commit which is often
cumbersome. You could instead utilize git rebase
for this matter.
This is command I run when I need to update my branch before merge.
--force
is required here because the commit you create on the branch is repeated and hence creating new SHA for the commit.
This will cause history between local and remote become different and need to be rewritten.
When you accidentally commit your changes, you have two choices: Reset to remote state which wastes your current work, or use this command
This will undo the local commit, assuming your last action is committing.
.gitkeep
for Empty DirectoriesYou couldn’t commit empty directories. But you could place .gitkeep
to keep the empty directory.
Git is a nice version control that has wide features and commands that you might not use. But once you learn basic of committing, branching, and merging, you will get used to these commands and tricks.