Command | Description | Ref |
---|---|---|
git init | Initialise a new Git repository | Getting a Git repository |
git clone <git transfer protocol> | Create a copy of a Git remote repo on your device | Getting a Git repository |
git status | Show the current state of the Git repo’s working directory and staging area | Git Internals 1 - Introduction to Git |
git log | Display the commit history of the repo, including the commit hash, and other related info. The --online flag gives you one line info per commit | |
git reset | Reset changes in the working directory | |
git revert | Revert a specified commit | |
git add | Stage changes for the next commit | |
git commit | Create a new commit in your Git repository | |
git branch | ||
git checkout | Mainly for switch between branches | |
git merge | Merge the target branch into the current branch | |
git push | Upload a local content to a remote repository | |
git fetch <remote> | Download commits, files, and references from a remote repository |
git add
The git add
command is used stage your files to the staging area.
git add
The command has several options that allow you to stage files in different ways.
This command adds a specific file to the staging area.
git add <file>, <more_files>
This command adds a specific directory to the staging area.
git add <directory>
add .
This command adds all changes in the current directory and all its subdirectories to the staging area.
git add .
The -A
or --all
flag allows you to stage all changes in the entire repository.
git add -A
add -u
The -u
flag allows you to stage only modified and deleted files that are already tracked by Git.
git add -u
add -p
The -p
or --patch
flag allows you to interactively stage changes from your working directory by breaking them into hunks, and selectively add parts of changes to the staging area instead of the entire file.
git add -p
git reset
git reset
is a multi-purpose command allows you to move both the HEAD
and branch pointer to a specific commit (You will need to know beforehand how git branches work internally in Git Internals 3 - Git Branches), and this command also allows you to unstage files by removing them from the staging area.
reset —soft
The --soft
flag moves the HEAD
pointer to a specific commit but the staging area and the working directory remains unchanged.
git reset--soft <commit_hash>
reset —mixed
The --mixed
flag is the default flag of the git reset
command when no flag is specified. It moves the HEAD
and the current branch to the specified commit. The staging area is reset to match the commit, but the working directory is not affected.
git reset <commit_hash>
git reset --mixed <commit_hash>
reset —hard
The --hard
flag is the most direct option. It moves the HEAD
and the current branch to the specified commit. The staging area and the working directory are both reset to match the commit.
Destructive action
The hard reset can be destructive that any pending work that was hanging out in the staging area and the working directory will be lost. Any uncommitted changes that were lost due to the
reset --hard
cannot be recovered.
git reset --hard <commit_hash>
Reset staged files
You can use the git reset
to remove files from the staging area, the changes to files are not lost, but those files they are no longer marked for inclusion in the next commit.
You can unstage all changes in a file using the command using the command below.
git reset <staged_file>
You can unstage all the files in the staging area using the command below.
git reset
reset -p
You can use the -p
flag to unstage changes interactively by hunk, a hunk is a group of changes that are close enough to each other in a file that Git treats them as a single unit.
This command allows you to unstage specific hunks from files that have been staged.
git reset -p <file>
git commit
The git commit
command creates a new snapshot in your Git repository.
The -m
or --message
flag allows you to specify a commit message from the command line without opening an editor.
git commit -m "This is a commit message."
commit -a
The -a
or --all
flag stages all modified and deleted files (but not untracked files) and commit these files. This is equivalent to running both git add -u
followed by git commit
.
git commmit -a -m "This is a commit message"
commit —amend
The --amend
allows you to modify the most recent commit in your Git history. Instead of creating a new commit, this command rewrites the previous commit with updated information or content, as well as its commit message.
git commit --amend
You can update your commit message like below.
git commit --amend -m "Updated log message"
git checkout
The main use of git checkout
is to switch between branches.
git checkout <branch_name>
git checkout .
The git checkout .
discards all changes in the working directory, reverting files to their last snapshot state.
git checkout .
checkout -b
You can combine the branch creation git branch
and switch branch in a single command using the -b
flag.
git checkout -b <branch_name>
git push
This command is for sharing changes and collaborate with other developers in a same remote repository. You can learn more about Git remote branches in Git remote branch. You specify the remote repository and branch to which you want to push changes.
git push <remore> <local_branch>
You can push to a specific remote branch using the following command. This command directly updates the target remote branch, if the remote branch does not exist, Git will create it automatically; if the remote branch already exists and has diverged from the local branch, and there are conflicts, the push will be rejected unless the conflict is resolved or a force push is executed.
git push <remote> <local_branch>:<remote_branch>
push -u
The -u
or --set-upstream
flag sets the upstream (tracking) reference for the current branch. This means future git push
commands can be run without specifying the remote and branch.
git push -u <remote> <local_branch>
This command makes future pushes can be done with just git push
.
push -f
The -f
or --force
command forces the push even if it results in a non-fast-forward merge. This option is used when you need to overwrite changes on the remote branch.
Destructive action
Force push can overwrite changes on the remote repository and may result in loss of data and changes other collaborators might have made.
git push -f <remote> <local_branch>
push —delete
The --delete
flag allows you to delete branch or tag from the remote repository.
git push <remote> --delete <branch>
push —dry-run
The --dry-run
flag simulates the push operation without actually sending anything to the remote. It is useful for testing and verification.
git push --dry-run <remote> <local_branch>
Back to parent page: Git