Updated on 2023-07-14 GMT+08:00

Common Git Commands

Background

  • Git is a free and open-source distributed version control system. It can manage projects of any size in an agile and efficient manner.
  • With Git, you can clone a complete Git repository (including code and version information) from a server to a local computer, create branches, modify and commit code, and merge branches.

Commonly Used Commands

The following table describes the functions, formats, parameters, and examples of common Git commands.

Table 1 Common Git commands

Command

Function

Format

Parameter

Example

ssh–keygen –t rsa

Generate a key

ssh–keygen –t rsa –C [email]

email: indicates an email address.

Obtain the key file id_rsa.pub from the .ssh folder in drive C.

ssh–keygen –t rsa –C "devcloud_key01@XXX.com"

git branch

Create a branch

git branch [new branchname]

new branchname: indicates the name of the new branch.

Create a branch:

git branch newbranch

git branch –D

Delete a branch

git branch –D [new branchname]

new branchname: indicates the name of the new branch.

Delete a local branch:

git branch –D newbranch

Delete a branch in the remote repository:

git branch –rd origin/newbranch

Remove branches that have been deleted in the remote repository:

git remote prune origin

git add

Add a file to the index

git add [filename]

filename: indicates the name of the file to be added.

Add a file to the index:

git add filename

Add all modified and new files to the index:

git add .

git rm

Delete a local directory or file

git rm [filename]

filename: indicates the name of the file or directory to be deleted.

Delete a file or a directory:

git rm filename

git clone

Clone a remote repository

git clone [VersionAddress]

VersionAddress: indicates the URL of the remote repository.

Clone a jQuery repository

git clone https://github.com/jquery/jquery.git

A directory is generated on the local computer. The name of the directory is the same as that of the cloned repository.

git pull

Pull the branch in the remote repository to the local computer and merge it with a specified local branch

git pull [RemoteHostname] [RemoteBranchname]:[LocalBranchname]

-

Pull the next branch from the remote repository and merge it with the local master branch.

git pull origin next:master

git diff

Compares files, branches, directories, or versions

git diff

-

Compare the current branch with the master branch:

git diff master

git commit

Commit files

git commit

-

Add a commit message:

git commit –m "commit message"

git push

Push files to the remote repository

git push [RemoteHostname] [LocalBranchname] [RemoteBranchname]

-

If the remote branch name is not specified, the local branch is pushed to the remote branch that it tracked (The two branches usually share a name). Such a remote branch will be created if it does not exist.

git push origin master

The local master branch is pushed to the master branch in the remote repository. If the latter does not exist, it will be created.

git merge

Merge branches

git merge [branch]

branch: indicates the name of the source branch

Assuming that the current branch is the develop branch. The latest commit to the master branch is merged to the develop branch.

git merge master

git checkout

Check out a branch

git checkout [branchname]

branchname: indicates the name of the branch to be switched to.

Check out the master branch:

git checkout master

git log

List the log

git log

-

List all logs:

git log –-all

git status

Check the status

git status

-

git status

git grep

Search for a character string

git grep

-

Check whether there is any character string containing hello:

git grep "hello"

git show

Display objects or revisions

git show

-

  • git show v1

    The revisions attached with the v1 tag are displayed.

  • git show HEAD

    Display the last commit of the current branch.

  • git show HEAD^

    Display the first parent of the last commit of the current branch.

  • git show HEAD~4

    Display the ancestor four generations prior to the last commit of the current branch.

git stash

Commands related to stashes

git stash

-

  • git stash

    Saves and restores the work progress.

  • git stash list

    Lists all stashes.

  • git stash pop

    Restore the latest stash and remove it from the stash list.

  • git stash apply

    Restore the latest stash but not remove it from the stash list.

  • git stash clear

    Clear all stashes.

git ls-files

View files

git ls-files

-

  • git ls-files –d

    View deleted files

  • git ls-files –d |xargs git checkout

    Restore deleted files

git remote

Perform operations on the remote repository

git remote

-

  • git push origin master:newbranch

    Create the master branch in the remote repository and push changes to it.

  • git remote add newbranch

    Create the master branch in the remote repository and push changes to it.

  • git remote show

    List the number of remote repositories

  • git remote rm newbranch

    Delete a new branch from the remote repository

  • git remote update

    Update branches of all remote repositories