Skip to content

mdmujtabaraza/git-cheatsheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

First Steps after installing git

git config --global user.name USERNAME
git config --global user.email EMAIL

Get the list of global configurations

git config --global --list
git config --global -e
cat ~/.gitconfig

Get help on any command

git help COMMAND_NAME
git COMMAND_NAME --help

Config P4Merge as Default Merge Tool and Diff Tool

In Windows Add P4Merge installation directory as path to environment variables

git config --global merge.tool p4merge
git config --global mergetool.p4merge.path "C:/Program Files/Perforce/p4merge.exe"
git config --global mergetool.prompt false

git config --global diff.tool p4merge
git config --global difftool.p4merge.path "C:/Program Files/Perforce/p4merge.exe"
git config --global difftool.prompt false

Basics

Git States

There are 3 Local states:

  • Working Directory
    Contains all files and folders for the application which may or may not be managed by Git. Either way, Git is aware of those files.
  • Staging Area
    Used to prepare for the next commit. Files are moved from modified working directory state to the staging area.
  • Repository (.git folder)
    Contains all the commited or saved changes to the Git repo. Anything here is part of Git's history.

Consider this 4th state:

  • Remote
    It's another repo with its own 3 states internally.

1. Initialize Project / Repository

git init PROJECT_NAME
git init .

2. Repository Status

git status

3. Move files to Staging Area

git add FILE_NAME
git add ./FOLDER_NAME
git add .
git add -A
git add -u

4. Move files into .git repo folder (Commit Changes)

git commit
git commit -m "COMMIT MESSAGE"

5. Commit History

git log
git log --oneline --graph --decorate --all

git show
git show COMMIT_ID
git show TAG_NAME

6. Express Commit

git commit -am "COMMIT MESSAGE"

7. Backing out / Restoring Changes

git restore .
git restore FILE_NAME/FOLDER_NAME

git restore --staged .
git restore --staged FILE_NAME/FOLDER_NAME

8. Git Aliases

git config --global alias.hist "log --oneline --graph --decorate --all"

9. Rename and Delete using Git

git mv ORIGINALFILE_NAME CHANGEDFILE_NAME

git rm FILE_TO_REMOVE

Advanced

Branching and Merge Types

  • Branch = Timeline of Commits
  • Branch Names are Labels
    • Deletion Removes Label Only
  • Default Branch is master branch

3 types of Merge

  • Fast Forward Merge
    Simplest, like never be branched, can be disabled
  • Automatic Merge
    non-conflicting merge detected, preserves both timelines, merge commit on destination.
  • Manual Merge
    automatic merge not possible, conflicting merge state, changes saved in merge commit.

Special Markers

  • Like pointers
  • HEAD
    • Points to last commit of current branch
    • can be moved

10. Difference between two commit points

git hist

git diff
git diff COMMIT_ID COMMIT_ID
git diff COMMIT_ID HEAD
git diff BRANCH_NAME BRANCH_NAME

git difftool
git difftool COMMIT_ID COMMIT_ID git diff BRANCH_NAME BRANCH_NAME

11. Branching

git branch
git branch -a

git branch BRANCH_NAME
git checkout -b BRANCH_NAME

git checkout BRANCH_NAME

git branch -d BRANCH_NAME

12. Merging

git merge BRANCH_NAME

git mergetool

13. Tags

git tag TAG_NAME

git tag --list

git tag -d TAG_NAME

git tag -a ANNOTATED_TAG_NAME -m "TAG_MESSAGE"
git show ANNOTATED_TAG_NAME

14. Stashing (Saving Work in Progress)

git stash

git stash list

git stash pop
git stash apply git stash drop

15. Reset and Reflog (Time Travel)

git hist

git reset COMMIT_ID --soft
git reset COMMIT_ID --mixed
git reset COMMIT_ID --hard

git reflog

Working with GitHub

16. Cloning a Repository

git clone REPO_URL
git clone REPO_URL FOLDER_NAME

17. List all remotes

git remote -v

18. Pushing on Remote

git push REMOTE_NAME BRANCH_NAME

19. Fetch and Pull

git fetch
git status

git pull

20. Set a new remote url

git remote -v
git remote set-url REMOTE_NAME REMOTE_URL
git remote show REMOTE_NAME

21. Push Local Branch

git push -u REMOTE_NAME BRANCH_NAME

22. Compare and Pull Request Locally

git status git checkout DEFAULT_BRANCH
git pull
git merge BRANCH_NAME
git push
git branch -a git branch -d BRANCH_NAME
git branch -a git fetch -p

23. Locally switch to a branch on GitHub

git branch -a git fetch
git branch -a git checkout REMOTE_BRANCH_NAME
git branch -a git push

24. Deleting Branch References

git status git checkout DEFAULT_BRANCH
git pull --all
git merge BRANCH_NAME
git push
git branch -a git branch -d BRANCH_NAME
git branch -a git push REMOTE_NAME :BRANCH_NAME

25. Pull with Rebase

  • Some Commits on remote repo
  • Some Commits on local repo
  • Both diverged after fetch
  • Do a rebase pull to stay ahead of remote

git fetch git status
git pull --rebase git hist

26. Changing the Default Branch

  • Change default branch from website
  • Clone a new repository locally git branch -a