Some commonly used git commands.

Initialize Git Local Repository Link to this heading

From local folder Link to this heading

$ git init

From online repository Link to this heading

$ git clone url

General git update routine Link to this heading

Suppose you have edited some file in repository, and ready for commitment. To update file in stage,

  • $ git add . To add all file changes. (Including add, delete and change)
  • $ git add filename To specific a file updata to stage.

After file changes have been recoreded in stage, you can check these changes via

  • git status Provides a list of change file. This command will also list change in this folder that have NOT been add to stage.
  • git diff filename Provides a detailed change of a specific file.

To commit these changes to local repository, use

git commit -m "commit message" Commit changes to local repository. The -m flag is used to add a commit message.

There are some other useful flags for git commit command.

  • -a Commit all changes, but not including new files.
  • -v Show the difference between the last commit and the current commit.

Git log (Version Control) Link to this heading

To reset to a previous version, you need to know the version code of that version. To check all version code, use

git log Show all commit history.

git log --pretty=oneline Show all commit history in one line.

git log --graph Show all commit history in graph.

git log --pretty=oneline --graph --decorate --all Show all commit history in graph with branch name.

Git reset (Version Control) Link to this heading

Reset to a previous version Link to this heading

  • git reset --hard HEAD^ Reset to the previous version. HEAD means the current version, HEAD^ means the previous version, HEAD^^ means the previous two versions, and so on.
  • git reset --hard HEAD~n Reset to the previous n version.
  • git reset --hard commit_id Reset to a specific version. commit_id is the version code of the version you want to reset to.

To check all version code, use git reflog.

Reverse changes before commit Link to this heading

  • In files manually.

  • Commit then reset (not elegant).

  • Delete changes in workspace.

    1. git status To check the difference.
    2. git checkout -- filename Reverse all change in current workspace.

    git checkout -- . Reverse all change in current workspace. If there is a previous version in stage, this command will reverse to that version. If not, this command will reverse to the latest version in the local repository.

Git branch Link to this heading

Generally, before you start a new feature, you should create a new branch. After the feature is completed, merge the branch into the main branch.

The temporary branch is only used to develop a new feature. After the feature is completed, it will be merged into the main branch, and then the temporary branch will be deleted. The branch is ususally named “dev”.

Create a new branch Link to this heading

git branch branch_name Create a new branch.

git checkout branch_name Switch to a new branch.

git checkout -b branch_name Create a new branch and switch to it.

git branch -m old_branch_name new_branch_name Rename a branch.

Merge branch Link to this heading

git merge branch_name Merge a branch into the current branch.

If there is a conflict, you need to manually modify the conflict file, and then add it to the stage, and then commit it.

Delete branch Link to this heading

git branch -d branch_name Delete a branch.

Check branch Link to this heading

git branch Check all branch.

git branch -v Check all branch with last commit message.

git branch -a Check all branch, including remote branch.

Git remote Link to this heading

Add remote repository Link to this heading

git remote add origin url Add a remote repository.

Push local repository to remote repository Link to this heading

git push origin branch_name Push local repository to remote repository.

git push -u origin main Push local repository to remote repository. -u flag is used to set the default push branch.

git push -f origin main Push local repository to remote repository. -f flag is used to force push. This command will overwrite the remote repository.

Clone remote repository Link to this heading

git clone url Clone a remote repository.

Check remote repository Link to this heading

git remote -v Check all remote repository.

Manage submodules Link to this heading

In some project, you could include other project as submodule. On github, you can see the submodule as a link to another repository.

  • A git repository should not be included in another git repository. I can only be included as a submodule.
  • If you want to edited files in submodule, you cancel the link and clone the submodule as a local repository.
  • When pushed to remote repository, the submodule will be pushed as a link, instead of the whole repository.

Add submodule Link to this heading

git submodule add url Add a submodule.

git submodule add url path Add a submodule to a specific path.

Update submodule Link to this heading

git submodule update --init --recursive Update submodule.

Git stash Link to this heading

When you want to switch to another branch, but there are some uncommitted changes in the current branch, you can use git stash to save the current workspace, and then switch to another branch. After switching to another branch, you can use git stash pop to restore the workspace.

git stash Save the current workspace.

git stash list Check all stash.

git stash apply Restore the last stash.

git stash apply stash@{n} Restore the n-th stash.

git stash drop Delete the last stash.

git stash drop stash@{n} Delete the n-th stash.

git stash pop Restore the last stash and delete it.

git stash pop stash@{n} Restore the n-th stash and delete it.

git stash clear Delete all stash.