A basic cheatsheet that consists of the most commonly used and basic commands for Git version control!
A basic cheatsheet that consists of the most commonly used and basic commands for Git version control!
VC Systems help us with:
diff file1 file2
diff -u file1 file2
-u
flag shows the additions and removals made in the second file, in a more organized way.
diff -u old_file new_file > change.diff
patch old_file < change.diff
changes.diff
file to the old_file
.Common first commands in Git are:
git init
: Initializes an empty Git repository in the current working directorygit add filename
: Lets the specified file be tracked by Gitgit add .
: Adds all the files in the current working directory and subdirectories in the tracking radar for Gitgit commit
: Commits our changes to the local repositorygit status
: Tells us which category our file is in. Files used by Git can be in one of the following three categories:
Modified -> Staged -> Committed
git add
command)Other basic commands and information are:
git commit -a
: Shortcut to stage and commit changes to tracked files in one command and step.git commit -m "Message"
: One-line commit messagegit log -p
: Gives information about patch.git log --stat
: Shows stats about changes in commit.git diff
: Similar to diff -u
command, shows unstaged changesgit add -p
Shows unstaged changes and asks if you want to stage itgit diff --staged
: Shows staged changesgit rm file_name
: Deletes file specified.git mv old_name new_name
: Renames the filegit checkout
: reverts changes to modified files before they are changedgit reset
: reverts staged changesgit commit --amend
: edits previous commit and overwrites the previous commit with edited versionNote: Do not amend public commits!
git revert HEAD
: creates new commit with inverse changesgit revert commit_ID
: reverts the commit specifiedgit branch new_branch_name
: creates new branch with name specifiedgit checkout branch_name
: switches to the branch specified (changes where HEAD points to)git checkout -b new_branch_name
: one command to create a new branch and switch to it in one stepgit branch -d branch_name
: deletes branch specifiedgit merge branch_to_be_merged
: merges branch specified to the branch HEAD is currently pointing togit log --graph --oneline
: summarized view of commit historygit merge --abort
: prevents merge if conflicts arisegit clone link_to_repo.git
: gets copy of remote repository onto your local machinegit config --global credential.helper cache
: caches remote repo’s credentials in local machine for 15 minutesgit remote
: lists remote repositoriesgit fetch
: downloads specific objects from remote and doesn’t mergegit pull
: downloads updates from the remote branch and mergesgit pull -a
: downloads updates from all remote branches and mergesgit rebase branch_name
: does fast-forward merge of current branch with the specified branchgit push -u origin
: pushes changes to the remote object origin, in accordance with current working branch.git rebase -i
: Interactive rebasing, offers complete control over the branch’s commit history. Helps clean up messy historygit push -f
: Forces a push to the remote repositoryWorkflow of Pull Request: