Skip to content

kinxyo/Git-Cheats

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 

Repository files navigation

Git-Cheats

Important

Replace filename with period (.) to include all files.
Examples are wrapped in blockquotes.
You can add multiple commands in one line by seprating them with semi-colon (;)


Basics & Commits

View All Files (Including Hidden)

ls -a

View Inside Folder:

ls _foldername_

ls .git

Status of Repository:

git status

Note

Unstaged Files are Red.
Staged Files are Green.

Stage Files:

git add _filename_

Unstage Files:

git restore --staged _filename_

Commit file:

git commit -m "_yourmessage_"

View Commit History

git log

Note

Press q to exit it.


Recovering files:

Deleted Files

git restore (filename)

Note

Each commit has a commit ID which can be found in Commit History.
Resetting will unstage all subsequent commits; do a git stash prior to save them.

Restore Point

To reset local repository to a particular point with reference of a commit

git reset _commit-id_

Hold/Stall Changes

When you neither want to commit the changes nor lose the them:

git stash

All staged and unstages changes will be saved to stash. You can bring them back to their respective place by:

git stash pop

Delete Stash:

git stash clear

Remote Repository

Setting Origin

Just like how we assign names to phone numbers in contact list, same way we set origin for identifying repository.

git remote add origin _repository-url_

Pushing to Repo:

git push origin _branchname_

When changes between local repo & remote repo aren't synced:

git push origin branchname -f

View All Repo(s) Associated

git remote -v

Remove all pushed files

git rm --cached -r .

Creating Branch

git branch _branchname_

Switching to Branch

git checkout _branchname_

Note

head -> points towards which branch is currently selected

Update Changes on Local Repo (all files)

git fetch --all
git pull origin _branchname_

Update Changes on Local Repo (single file)

git fetch --all
git checkout origin/master -- _filePath_

Note

"origin/master" is subject to change depending on where you're working.

Delete Branch (local)

git branch -d _branch_

Delete Branch (remote)

git push origin --delete _branch_

Merging

git checkout _branch-you-wanna-merge-in_
git merge _branch-to-merge_

Fork

Getting Started

git clone _forked-repository-url_
git remote add upstream _repository-url_
git branch _branchname_
git checkout _branchname_

Note

Origin will be set by default and will refer to the forked version of original repository & Upstream will refer to the original repository.

Merge forked repo with original repo

You can't push to uptream directly so you'll have to make a Pull Request

Updating Changes

  1. Automatic
    git pull upstream _branchname_
    
  2. Manually
    git fetch --all --prune
    git checkout _branch-you-wish-to-sync_
    git reset --hard upstream/main
    

Note

--prune includes deleted files as well.

Setup account (?)

git config --global user.email "you@example.com"
git config --global user.name "Your Name

Miscellaneous

View contributions

git shortlog -s -n