Skip to content

Instantly share code, notes, and snippets.

@brijesh-deb
Last active January 6, 2023 15:33
Show Gist options
  • Save brijesh-deb/bcbe562fcdc0afcfc375632efc166cca to your computer and use it in GitHub Desktop.
Save brijesh-deb/bcbe562fcdc0afcfc375632efc166cca to your computer and use it in GitHub Desktop.
#CheatSheet #Git

Git Cheat Sheet

Installation

  • Download exe (Git-2.7.1.2-64-bit.exe)
  • Install exe
  • Edit environment variable (PATH) to include C:\Program Files\Git\cmd

Commands

  • Check git version: git --version
  • Clone/download code
    • git clone <<repository_url>>
    • git clone -b <remote_repo> [ clone from a specific remote branch]
  • List uncommited changes in current branch: git status (inside the repository folder)
  • List all branches: git branch -a
  • Create a branch (locally) and switch to it: git checkout -b <>
  • Switch to a brach: git checkout <>
  • Commit a change in current branch: git commit -am "your comment"
  • Delete a branch
    • git branch -d <branch_name> [ local branch]
    • git push origin --delete <branch_name [remote branch]
  • Push a local branch to remote github and synch up: git push --set-upstream
  • Check difference between local and remote branches: git diff master origin/master
  • Check current git config: git config --list
  • Display history of changes to a file: git log [file name]
  • Restore a committed file to a earlier version
    • check history of file: git log [file name]
    • Restore to a specific commit version: git reset [commit hashcode]
    • File will be reset to that version, all changes made after that will be stage area
  • Save uncommited changes for later use
    • save uncommitted changes in stash: git stash
    • list all stashes: git stash list
    • retrieve stashed change: git stash pop stash@{X}
    • clear stashed changes: git stash clear

Adding an existing project in github from command prompt

  • Initialize local directory as Git repository: git init
  • Delete a file: git rm <>
  • Remove files that needs to be added in .gitignore: git rm --cached node_module/*
  • Create .gitignore file: touch .gitignore
  • Add .gitignore: git add .gitignore
  • Check all uncommitted changes in local repository: git status
  • Discard uncomitted local changes made to a file: git restore [file name]
  • Add files in the directory to local repository. This stages the files for first commit: git add .
  • Commit the files: git commit -m "Initial commit"
  • Check branches: git branch -a
  • Add the URL of the remote repository: git remote add origin [remote repository URL]
  • Remove URL of remote repository: git remote rm origin
  • Verify remote URL: git remote -v
  • Rename remote branch: git remote rename [origin] [upstream]
  • Remove link to a remote repo: git remote remove [remote_link_name]
  • Push local files to remote repository: git push origin master (username = XXXXX password=Public access token)

Pushing local changes to remote

  • Check local changes: git status
  • Commit changes locally: git commit -am "message"
  • Push changes to remote: git push origin master

Create a new local branch and push to remote

  • Create a branch (locally) and switch to it: git checkout -b <>
  • Push the branch to remote: git push origin <>

Merge code

  • Switch to the branch where u want to merger: git checkout master [ switch to master where develop branch needs to be merged]
  • Merge code: git merge develop [merge develop branch into master]

Pull changes from Remote to local

  • Check local branch and go to branch which is to be synced with Remote:
  • git pull [remote branch url]

Disassociate project from Git

  • Go to project directory
  • del /F /S /Q /A .git
  • rmdir .git

Generate public access token

  • Settings > Developer Setting > Personal access tokens > Generate new token
  • Select all check box
  • Save the token to use later

Add an existing project in github thru SourceTree

  • Go to local folder where project code is there
  • Make it a git project: git init
  • In source tree
    • Clone/new > Add Working Copy
      • Ignore the files that are not to be added in Git
      • Make initial commit (files will be added in local master)
      • Git Flow > create local branches (develop and master)
      • Now local Master & Develop branches should be in synch
      • Add remote branch
        • Repository > Repository Settings > Remotes
          • Remote name : origin
          • URL/Path:
      • Push local develop and master branch to remote
      • Check in github url, these 2 branches should get added

Create a folder

  • Go to the folder inside which you want to create another folder
  • Click on New file
  • On the text field for the file name, first write the folder name you want to create
  • Then type /. This creates a folder
  • Finally, give the new file a name (for example, .gitkeep)
  • Finally, click Commit new file

Merging

Fast-Forward

  • When you try to merge one commit with a commit that can be reached by following the first commit's history in the same branch, Git simplifies things by moving the pointer forward because there is no divergent work to merge together. This is called "fast-forward"
	c0 -> c1 -> c2 (master) ->c4 (hotfix)	[Pre Merger]
	c0 -> c1 -> c2 ->c4 (master, hotfix)	[Post Merge]

Basic Merger

  • Say we have branches like this
	c0 -> c1 -> c2 ->c4 (master)	
		    | 
		    |  ->c3 ->c5(issuefix)	
  • If we commit issuefix branch into master, Git does a 3-way merge, using 2 snapshots pointed to the branch tips(c4,c5) and common ancestor of both (c2).
  • Instead of moving the branch pointer forward, Git creates a new snapshot(c6) that results from this 3-way merge and automatically create a new commit that points to it.
	c0 -> c1 -> c2 ->c4 ---------------->c6(master)	
		    | 			     | 	
		    |  ->c3 ->c5(issuefix) ->|	

Rebase

Branching strategy

Troubleshooting

Git uses old credential stores in windows cache for commands

Problem
  • Give clone/Push command from command prompt
  • 403 error is shown with message - unable to access [repo url]
Solution
  • Go to Control Panel > User Accounts > Credential Manager
  • Remove Git specific credentials
  • Give clone/push command from command prompt
  • Now git will ask for user name and password
  • Give use name and personal access token
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment