Git Cheat Sheet
1. Git Configuration
- Git config
Retrieve and modify configuration variables that manage every aspect of Git's appearance and functionality.
Set the name:
$ git config --global user.name "User name"
Set the email :
$ git config --global user.email "Johnxyz@gmail.com"
Set the default editor:
$ git config --global core.editor vim
Check the setting:
$ git config --list
- Git alias
Create an alias for each command:
$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status
2. Starting a project
- Git init
Initialize a local repository:
$ git inti
- Git clone
Clone a local copy of the server repository:
$ git clone
3. Local Changes
- Git add
Add a file to staging (Index) area:
$ git add Filename
Add all files of repo to staging (Index) area:
$ git add*
- Git commit
Save a permanent snapshot of the file in the version history with a commit message.
$ git commit -m "commit Message"
4. Track changes
- Git diff
Monitor the changes that haven't been added to the staging area:
$ git diff
Track the changes that have been staged but not yet committed:
$ git diff --staged
Monitor the changes made after committing a file:
$ git diff HEAD
Compare the differences between two commits:
$ git diff Git Diff Branches:
$ git diff <branch 2>
- Git status
Show the status of the working directory and staging area.
$ git status
- Git show Shows object:
$ git show
5. Commit History
- Git log
Show the latest commits and the status of the HEAD:
$ git log
Show the output with each commit listed on a separate line:
$ git log -oneline
Show the files that have been changed.
$ git log -stat
Show the modified files along with their paths.
$ git log -p
- Git blame
Show the changes made to each line of a file.
$ git blame <file name>
6. Ignoring files
- .gitignore
Define untracked files for Git to ignore by creating a '.gitignore' file.
$ touch .gitignore
List the ignored files:
$ git Is-files -i --exclude-standard
7. Branching
- Git branch Create branch:
$ git branch List Branch:
$ git branch --list Delete a Branch:
$ git branch -d Delete a remote Branch:
$ git push origin -delete Rename Branch:
$ git branch-m
- Git cheknow
Switch between different branches in a repository.
Move to a specific branch:
$ git checkout
Create and switch to a new branch:
$ git checkout -b
Checkout a Remote Branch:
$ git checkout
- Git stash
Switch branches without committing by stashing your current work:
$ git stash
Save stashes with a descriptive message:
$ git stash save ''''
View the list of saved stashes:
$ git stash list
Re-apply the changes you just stashed:
$ git stash apply
Monitor the stashes and the changes they contain:
$ git stash show
Re-apply the commits from before:
$ git stash pop
Remove the most recent stash from the queue:
$ git stash drop
Delete all existing stashes at once:
$ git stash clear
Stash your work and switch to a separate branch:
$ git stash branch
- Git cherry pic
Apply the changes from a specific existing commit:
$ git cherry-pick
8. Merging
- Git merge
Merge the branches:
$ git merge
Merge the specified commit into the currently active branch:
$ git merge
- Git rebase
Combine a sequence of commits from different branches into a single final commit:
$ git rebase
Resume the rebasing process:
$ git rebase - continue
Abort the rebasing process:
$ git rebase --skip
- Git interactive rebase
Perform various operations such as editing, rewriting, reordering, and more on existing commits.
$ git rebase -i
9. Remote
- Git remote
Verify the configuration of the remote server:
$ git remote -v
Add a remote repository:
$ git remote add
Fetch the data from the remote server:
$ git fetch
Remove a remote connection from the repository:
$ git remote rm
Rename a remote server:
$ git remote rename
Display more details about a specific remote:
$ git remove rename
Change remote:
$ git remote set-url
- Git origin master
Push data to the remote server:
$ git push origin master
Pull data to the remote server:
$ git pull origin master
10. Pushing Updates
- Git push
Transfer commits from your local repository to the remote server:
$ git push origin master
Force push data:
$ git push -f
Remove a remote branch using the push command:
$ git push origin -delete edited
11. Pulling updates
- Git pull
Pull data from the server:
$ git pull origin master
Pull a remote branch:
$ git pull
- Git fetch
Retrieve branches and tags from one or more repositories by fetching the remote repository:
$ git fetch < repository Url >
Fetch a specific branch:
$ git fetch
Fetch all branches at once:
$ git fetch -all
Sync the local repository:
$ git fetch origin
12. Undo changes
- Git revert
Revert the changes:
$ git revert
Revert a specific commit:
$ git revert
- Git reset
Reset the changes:
$ git reset -hard
$ git reset - soft:
$ git reset --mixed
13. Removing files
- Git rm
Delete the files from both the working tree and the index:
$ git rm <file Name>
Remove files from Git while keeping them in your local repository:
$ git rm --cached