Summary of Commonly Used Git Commands

Common Commands

git init

Initialize the repository.

1
git init

git status

View the current status of the repository.

1
git status

git add

Add files to the staging area.

1
2
3
4
5
# Add the specified file
git add <file_name>

# Add all files
git add .

git commit

Commit the staging area to the local repository.

1
git commit -m "Commit message"

git log

View submission history.

1
2
git log
git log --all --graph --decorate

git reset

Undo the changes.

  • Revert the most recent commit
1
git reset --hard HEAD^
  • Fallback to a specific version
1
git reset --hard <commit_id>

git diff

Compare the workspace with the staging area.

1
git diff

git checkout

  • Switch from the current branch to the specified branch
1
git checkout <branch_name>

For example, switch the working directory to the main branch:

1
git checkout master 
  • Create a new branch and switch to this new branch immediately
1
git checkout -b <new-branch-name>

git branch

  • Create a new branch
1
git branch <new_branch>
  • Delete the branch
1
git branch -d <branch_name>

git merge

Merge other branches into the current working branch.

git merge <branch_to_merge>

For example, merge the modifications of the dev branch into the current branch:

1
git merge dev

git push

Push the local branch to the remote.

1
git push origin <local_branch>

git pull

Pull the remote branch.

1
git pull origin <remote_branch>

git tag

  • Create a label
1
git tag <tag_name>
  • Push tags to the remote
1
git push origin <tag_name>
  • See all tags
1
git tag