Environment
- Git
- Version: 2.45.0
Commands
git add
Add file contents to staging.
git add <pathspec>
We can specify a file path for <pathspec>
.
Or we can add all files insted of a specific file.
git add .
git branch
List local branches.
git branch
We can use option --remotes
in order to list remote branches.
git branch --remotes
We can use -r
instead of --remotes
.
git branch -r
delete
Option --delete
can be used to delete a branch.
git branch --delete <branch>
We can use -d
instead of --delete
.
git branch -d <branch>
If a branch has not been fully merged into its upstream branch, we cannot delete it.
However, combined with option --force
, we can delete it irrespective of its merged status.
git branch --delete --force <branch>
We can use -f
instead of --force
.
git branch -d -f <branch>
Furthermore, we can use -D
instead of -d -f
.
git branch -D <branch>
git checkout
Switch current branch to another branch on local.
git checkout <branch>
If the branch specified by <branch>
does not exist, the switching will fail of course.
However, with option -b
, a new branch is created based on current branch.
git checkout -b <new-branch>
Or we can explicitly specify a base branch or commit hash as <start-point>
.
git checkout -b <new-branch> <start-point>
We can also specify a remote branch as base branch.
git checkout -b <new-branch> origin/<remote-branch>
git clone
Clone a repository into current directory.
git clone <repository>
The cloned repository is named origin
, and we specify that name as <repository>
when we execute git push
.
Instead of the name origin
, we can give it another name.
git clone --origin <name> <repository>
We can use -o
instead of --origin
.
git clone -o <name> <repository>
git commit
Apply changes to the repository.
git commit
We can use option --message
in order to write commit message.
git commit --message <msg>
We can use -m
instead of --message
.
git commit -m <msg>
git log
Show commit logs.
git log
git merge
Merge the commit history of a branch to current branch.
git merge <branch>
git push
Update remote branch using local branch.
git push <repository> <refspec>
<refspec>
can be specified in the format <src>:<dst>
.
git push <repository> <src>:<dst>
We can specify local branch for <src>
and remote branch for <dst>
.
git push <repository> <local-branch>:<remote-branch>
If the names <local-branch>
and <remote-branch>
are the same, we can be written once.
git push <repository> <branch>
force
If the commit history has been updated by git rebase
, the push might fail.
In this case, option --force-with-lease
can be used to overwrite the commit history of the remote branch.
git push --force-with-lease <repository> <refspec>
If someone else had updated the remote branch, even if we used --force-with-lease
, the push will fail.
Even if someone else has done, using option --force
instead of --force-with-lease
, we can update if we want.
git push --force <repository> <refspec>
We can use -f
instead of --force
.
git push -f <repository> <refspec>
git pull
Incorporate changes from a remote repository into the current branch.
git pull
git rebase
Reapply commits on the commit history of the specified branch.
git rebase <base-branch>
git reset
Reset file contents from staging.
This command is opposite of git add
.
git reset <pathspec>
We can specify a file path for <pathspec>
.
git revert
Revert changes in a commit.
git revert <commit>
We specify a commit hash as <commit>
.
git stash
Save uncommitted changes and revert working dir to match the HEAD commit.
git stash push
push
can be omitted.
git stash
To save paths that are not tracked in Git (e.g. newly added ones) as well, we can use option --include-untracked
.
git stash --include-untracked
We can use -u
instead of --include-untracked
.
git stash -u
Saved changes can be displayed by executing with list
.
git stash list
A saved changes are named stash@{n}
.
n
is an index starting from 0 and newer saved change has smaller index.
We can restore a change by specifying the name above.
git stash pop stash@{n}
A restored change is deleted from stash list.
If we want to keep that in the list, use apply
instead of pop
.
git stash apply stash@{n}
Then we delete it when we want.
git stash drop stash@{n}
git status
Show the working tree status include following.
Changes to be committed.
paths that have differences between the staging file and the current HEAD commit.Changed not staged for commit.
paths that have differences between the working tree and the staging file.Untracked files.
paths in the working tree that are not tracked by Git.
The former one is committed when git commit
is executed, but the latter two are not.
git status
git --version
Show the version of Git installed in a local environment.
git --version
We can use -v
instead of --version
.
git -v