Git Guide or Cheatsheet or Whatever

Posted by hori75 on February 27, 2022 · 6 mins read

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.

Overview of Git

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.

Git Remote Server

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.

Commands of Git

This is a list of git commands I have used.

Initializing a Repository

git init

This command is used to initialize a local repository. The .git folder is initialized by this command.

Cloning a Remote Repository

git clone <remote_url> <target_dir>

This clones a remote repository into a folder. An origin remote is set to able to pull and push to the remote repository.

Setting Remote Repository

git remote add <remote_name> <remote_url>
git remote set-url <remote_name> <remote_url>
git remote remove <remote_name>

This command is used to manage remote repositories it relates to.

Stage Changes

git add <files>

This command stages files to be commit.

Commit Changes

git commit
git commit -m <commit_message>
git commit -a

This commits changes staged using git add. You also could modify previous commit using -a.

Pull Changes from Remote Repository

git pull
git pull <remote> <branch>

This command pull commits from remote repository. If executed on different branch, it will create a merge commit to apply pulled commits.

Push Changes to Remote Repository

git push <remote> <branch>

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.

Status

git status

This command outputs an overview of your local repository. This includes current branch and uncommited changes if any.

Branch

git branch <new_branch_name>

This command creates a new branch for current branch. Keep in mind that you need to select the new branch using checkout.

Checkout

git checkout <branch_name>
git checkout -b <new_branch_name>

This command changes the current branch to specified branch. You could use -b to create new branch and checkout to the new branch.

Log

git log
git reflog

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.

Stash

git stash

This command stashes your uncomitted changes. Useful when you need to pull or change branch. To unstash, use git stash apply.

Rebase

git rebase <branch_source>

This adds commits added from source branch before repeating your commits on current branch.

Tips and Tricks

These are tips and tricks that could be used for convenience.

Updating Branch without Merge Commit

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.

git checkout master
git pull origin master
git checkout <branch_name>
git rebase master
git push --force origin <branch_name>

--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.

Reverting Recent Unpushed Commit

When you accidentally commit your changes, you have two choices: Reset to remote state which wastes your current work, or use this command

git reset HEAD~1 --soft

This will undo the local commit, assuming your last action is committing.

Use .gitkeep for Empty Directories

You couldn’t commit empty directories. But you could place .gitkeep to keep the empty directory.

Conclusion

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.