Skip to content

Instantly share code, notes, and snippets.

@manojjha
Last active July 28, 2021 07:33
Show Gist options
  • Save manojjha/f2d5abb616a0af6510faf5fe223fee04 to your computer and use it in GitHub Desktop.
Save manojjha/f2d5abb616a0af6510faf5fe223fee04 to your computer and use it in GitHub Desktop.

GIT CHEATSHEET

CREATE

From existing data
cd ~/projects/myproject
git init
git add .
From Existing repo
git clone ~/existing/repo ~/new/repo
git clone you@host.org:dir/project.git
   Default protocol is ssh
Remove repository from existing local data
mkdir repo.git && cd repo.git
git init --bare[--shared=group]

UPDATE

Fetch latest changes from origin
git fetch
this does not merge them
Pull latest changes from origin
git pull
 does a fetch followed by a merge
Apply a patch that someone sent you
git am -3 patch.mbox
 In case of conflict, resolve the conflict and 
git am --resolve

PUBLISH

Commit all local changes
git commit -a
Prepare a patch for other developers
git format-patch origin
Push changes to origin
git push [origin] [branch]
Make a version of Milestone
git tag<Version_name>

Branch

Switch to the BRANCH branch
git checkout <BRANCH>
Merge branch B1 into branch B2
git checkout <new> <base>
Delete a branch
git branch -d <branch>

REVERT

Return to the last committed state
git checkout -f | git reset --hard
you can not undo a hard reset
Revert the Last commit
git revert HEAD
Create a new commit
Revert specific commit
git revert $id
 creates a new commit
Fix the last commit
git commit -a --amend
 after editing the broken files
Checkout the ID version of a file
git checkout <ID> <file>

SHOW

Files changed in working directory
git status
Changes to tracked files
git diff
Changes between ID1 and ID2
git diff <ID1><ID2>
Hisotry of changes
git log
History of changes with the files changed
git whatchanged
Who changed what and when in a file
git blame<file>
A commit identified by ID
git show <ID>
A specific file from a specific ID
git diff <ID>:<FILE>
All local branches
git branch
star "*" marks the current branch
Search for patterns
git grep <pattern>[path]

master is the default development branch origin is the default upstream repository HEAD is the current branch

@manojjha
Copy link
Author

…or create a new repository on the command line
echo "# FLASK_EnrollmentAPP" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/manojjha/___REPONAME__.git
git push -u origin main
…or push an existing repository from the command line
git remote add origin https://github.com/manojjha/___REPONAME__.git
git branch -M main
git push -u origin main

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment