Git Cheat Sheet
Git Cheat Sheet
Basics
Command | Description |
---|
git –help | List help commands |
git –version | Get installed Git version |
git update-git-for-windows | Update the installed version of Git |
Configuration
Command | Description |
---|
git config -l | Lists information about your git configuration |
git config –global user.name “[firstname lastname]” | Set the name that will be attached to your commits and tags |
git config –global user.email “[email address]” | Set the e-mail address that will be attached to your commits and tags |
git config –global color.ui auto | Set automatic command line coloring for Git for easy reviewing |
Setup a project and clone to local
Command | Description |
---|
git init | Initialize a new local repository in the current directory |
git init “[project name]” | Initialize a new local repository in a new directory with the project name |
git clone “[project url]” | Downloads a remote repository with all the history into the current directory. |
Add changes
Command | Description |
---|
git add [file] | Add a file to staging |
git add . | Adds all files to staging |
git add -p | Walks through all unstaged changes and prompts if you want to stage or not |
git add fil* | Adds files starting with fil to staging |
Show differences
Command | Description |
---|
git diff | Display unstaged changes |
git diff –staged | Display staged changes |
Commit changes
Command | Description |
---|
git status | List which files are staged, unstaged, and untracked |
git commit | Commits files to staging but opens a text editor for the comment |
git commit -m “[comment”] | Commits all staged files with a comment |
git commit -a -m “[comment]” | Add and commit in one go (skips staging) with a comment |
Authentication
Work with remote repositories
Command | Description |
---|
git add remote [repository URL] | Adds a remote repository to your local repository |
git remote -v | List all remote repositories |
git remote show origin | Display more details about the remote repository |
git push | Upload all staged changes to the remote repository |
git pull | [Download all changes from the remote repository |
git remote set-url origin git@github.com:yourusername/yourrepository.git | switch to using the SSH URL |
Remove changes
Command | Description |
---|
git checkout [filename] | Revert an unstaged file |
git reset HEAD [filename] | Revert a staged file |
git reset HEAD -p | Walks through all staged files and asked if you want to revert |
Revert a commit
Command | Description |
---|
git revert HEAD | Create a new commit that is the opposite of everything in teh given commit |
git revert [commit id] | Revert to an old commit using its commit iD |
Delete files
Command | Description |
---|
git rm [filename] | Deletes a file from the current working tree |
Change existing file
Command | Description |
---|
git mv [old file] [new file] | Renames the file |
Branches
Command | Description |
---|
git branch | List all of the branches in your repo |
git branch [new_branch] | Create a new branch |
git checkout [different_branch] | Use a new or different branch |
git checkout -b [new_branch] | Create a new branch and use it immediately |
git branch -d [branch] | Delete a branch |
git merge [branch] | Merge the history of the current branch with the given branch |
Logs
Command | Description |
---|
git log | Display all logs |
git log [-n] | Display all logs limited by [-n] |
git log -p | Displays all logs and files and changes |
git log –stat | Displays all logs and statistics |
References
50 Commands you should know
Last modified July 21, 2024:
update (e2ae86c)