Contents:
- What is Git
- Git commands to:
- Conclusion
What is Git?
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
Git commands to Prepare Repository:
Initialise Repository
git init
Create a new empty repository in the current directoryClone Repository
git clone <url> <dir>
Clone repository from the url named dir
Move to Contents
Git commands for Configuration:
Show Configurations
git config --list
Show current configurationsSet User Name
git config user.name <"name">
Set comitter usernameSet User Email
git config user.email <"email">
Set commiter email addressUnix/Windows EOL Conversion
git config core.autocrlf true
Automatically convert between Unix and Windows end of the line on commit and checkoutEOL Conversion on Save Only
git config core.autocrlf input
Convert CRLF to LF only when committing, use original line ending on checkoutChange Editor
git config core.editor <editor>
Change text editor for writing commit messageGet Configuration
git config --get <config-name>
Get configuration value for the configuration nameUnset Configuration
git config --unset <config-name>
Unset configuration value for given configuration nameConfiguration Scope
git config [--global | --system] <name> <value>
-- local or none for current repo, --global for user scope, --system for system scope
Move to Contents
Git commands to Work With Repository
Show Status
git status
Show working directory status, what files changedAdd File to Index
git add <file>
Add file or directory to the index for the next commitAdd Modified Files
git add -u
Update modified files to the index, it will not add new filesAdd All Files
git add -A
Add all file or directory to the index for the next commitRemove File from Index
git restore --staged <file>
Remove file from index, file on current directory unaffectedDiscard Modification
git checkout -- <file>
Discard file modification, restore file from indexDelete File
git rm <file>
Delete file in the working directory and the indexCommit Changes
git commit -m ["commit message"]
Apply changes in the index with given commit messageReverse Commit
git commit --amend
Replace the last commit of the current branch with the current indexReset Author
git commit --amend --reset-author
Amend commit author and author-date to the committerChange Author
git commit --amend --author="Author Name <email>"
Amend commit author with given author name and email, author date unchangedChange Author Date
git commit --amend --date="2020-11-21T23:59:59"
Amend author date, use ISO 8601 format for convenienceShow Commit Log
git log [-n <number>]
Show commit logs, limit showing <number> commitsShow Short Log
git shortlog
Show shorter commit logShow Log Summary
git shortlog -s
Show even shorter commit count summaryDiff Working Directory and Branch
git diff <branch-name>
Show diff between a branch and working directoryShow Diff between Branches
git diff <branch-1> <branch-2>
Show diff between two branches
Move to Contents
Git commands to Manage Branch
Detach Branch
git checkout --detach
Detach HEAD from current branchCreate New Branch
git checkout -b <branch-name>
Create new branch starting from current headSwitch Branch
git checkout <branch-name>
Change active branchCheckout to Commit-Name
git checkout <commit-name>
Checkout to commit-name. Commit name can be branch name, commit hash, or relative HEAD referenceCheckout to Previous Commit
git checkout <HEAD~1>
Detach HEAD and switch to the previous commit. Where ~1 is the previous commit, ~2 is the previous 2 commitsReset Current Head
git reset --hard <commit-name>
Move current branch HEAD to commit-name. Changes in index AND working directory are discarded. Potentially causing commits after a commit-name loss. USE WITH CAUTIONList Branches
git branch [--list]
List local branchesList Remote Tracking Branches
git branch -r
List remote-tracking branchesList Branches and Commit Messages
git branch -v
List branches, hash, and commit messageSort Branches by Committer Date
git branch -v --sort=commiterdate
List branches, hash, and commit message sort by committer dateSort Branches by Author Date
git branch -v --sort=authordate
List branches, hash and commit messages, sort by author-dateList All Branches
git branch -a
List both local and remote-tracking branchesDelete Branch
git branch -d <branch-name>
Delete already merged branchForce Delete Branch
git branch -D <branch-name>
Delete branch, even unmerged. USE WITH CAUTIONMerge Branch
git merge <other-branch>
Merge other branches with current branch, fast-forward if possibleMerge Using Merge Commit
git merge --no-ff <other-branch>
Merge other branch, use merge commitInteractive Branch Rebase
git rebase -i <other-branch>
Rebase current branch with other branch interactively
Move to Contents
Git commands to Manage Tags
List Tags
git tag
List available tagsAdd Tags
git tag <tag-name>
Create new lightweight tag named <tag-name>Delete Tags
git tag -d <tag-name>
Delete a tag named <tag-name>
Move to Contents
Git commands to Stash Working Directory
Stash Working Directory
git stash save
Save working directory state to new stash and clean up the working directoryList Stashes
git stash list
List stashes, stash@{0} is last stashRestore Stash
git stash pop
Restore the last stash and apply it to the working directoryRemove Last Stash
git stash drop
Remove the last stash, saved state will be discardedClear Stashes
git stash clear
Remove all stashes, saved state will be discarded
Move to Contents
Git commands to Remote Repository
Show Remote Repositories
git remote -v
Show remote repositories name and URL for push fetchAdd Remote Repository
git remote add <remote-name> <url>
Add new remote repository with given remote namePush branch to remote
git push <remote-name> <branch-name>
Push local branch to remote repositoryDelete Remote Branch
git push --delete <remote-name> <branch-name>
Delete a branch from remote repositoryPush Tag
git push <remote-name> <tag-name>
Push single local tag to remote repositoryPush All Tags
git push --tags <remote-name>
Push all local tags to remote repositoryDelete Remote Tag
git push --delete <remote-name> <tag-name>
Delete a tag from remote repositoryFetch from Remote
git fetch <remote-name>
Update remote-tracking branches. Remote tracking branches are NOT automatically merged with local branches. Use git branch -r to list-remote tracking branchesPull from Remote
git pull <remote-name> <branch-name>
Retreve objects from remote-branches, and merge with current branch. USE WITH CAUTION
Move to Contents
Git commands for Repository Maintenance
Optimize Repository
git gc
Remove unnecessary files and references, optimize repository. It is a good idea to run gc periodicallyOptimize Repository if Required
git gc --auto
Check and run gc only if required, otherwise exit without doing gcCheck Repository
git fsck
Check integrity of objects in the repository
Move to Contents
Conclusion
I mentioned some of the most used git commands. To learn more about git commands, visit the official git documentation here.
Top comments (0)