A basic cheatsheet that consists of the most commonly used and basic commands for Git version control!
VC Systems help us with:
- maintaining a healthy codebase
- helps avoid quick fixes
- keeps historical copies
- keeps track of changes
diff file1 file2
diff -u file1 file2
- The output of this on the command line shows the differences in the two files.
- The
-uflag 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
- The first line of code here generates a file that contains all the changes made, and only the changes.
- The second command applies the changes generated in the
changes.difffile to theold_file.
Common first commands in Git are:
-
git init: Initializes an empty Git repository in the current working directory -
git add filename: Lets the specified file be tracked by Git -
git add .: Adds all the files in the current working directory and subdirectories in the tracking radar for Git -
git commit: Commits our changes to the local repository -
git 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- Modified files : Files that have been locally changed and saved in the computer.
- Staged files: Files that have been set for tracking by Git. (using the
git addcommand) - Committed files: Files that have had their changes saved in the local repository.
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 message- HEAD alias : Represents curently checked out snapshot of project
git log -p: Gives information about patch.git log --stat: Shows stats about changes in commit.git diff: Similar todiff -ucommand, shows unstaged changesgit add -pShows unstaged changes and asks if you want to stage itgit diff --staged: Shows staged changes
git rm file_name: Deletes file specified.git mv old_name new_name: Renames the file
git 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 version
Note: Do not amend public commits!
git revert HEAD: creates new commit with inverse changesgit revert commit_ID: reverts the commit specified
git 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 arise
git 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 repository
- Forking : Creates a copy of the given repository so that it belongs to our user
- Pull request : Way to send and suggest changes to original owner of repository
Workflow of Pull Request:
- Fork project and clone the fork
- Make changes locally and commit
- Push change while making new branch
- Open pull request on Github/GitLab and check if the changes can be merged (this shows automatically)