Skip to content

Instantly share code, notes, and snippets.

@hsayed21
Last active August 25, 2021 01:27
Show Gist options
  • Save hsayed21/3714dd7ef4fb233c7027e5df2f6f2f75 to your computer and use it in GitHub Desktop.
Save hsayed21/3714dd7ef4fb233c7027e5df2f6f2f75 to your computer and use it in GitHub Desktop.

Git Power


Basics Git

# Clone Project From Github
git clone https://github.com/test/test.git 

# Create Repository From Existing Project
git init

# Show Status Of Added Or Moddifed Files
git status

# Add Files To Staged Area
git add .
git add [filenames]

# Show all file in stage area
git ls-files -s   
find .git/objects -type f

# Change Files From Staged To UnStaged Area
git rm --cached [filename] # from stage to unstage 
git rm --cached -r . # unstage all files
git restore --staged [filename]
git restore --staged *
git reset head [filename]

# Remove Files From Repo
git rm filename	// remove from working area
git rm -f filename // remove from staged area
git mv filename filename2 #rename file

### Undo To Previous Change
git checkout <filename> # undo changes of file from last commit | reset your changes on file
git checkout [SHA] filename # reverting specific filename from last commit | the same above
git checkout [SHA] -- filename
git checkout [SHA] # reverting all files this hash commit with some changes
git commit --amend -m "comment" # to save changes on the same commit

# Make branch from previous commit
git checkout [SHA] 
git switch -c <new-branch-name>

# Commit Repo
git commit -m "comment"
git commit -am "message" # add then commit
git commit --amend -m "asd" # change previous commit with current changes
git commit --amend --author="HS <hs@gmil.com>"
# Remove Commit | Resetting the head
1. Soft Reset
# move pointer to SHA and last head goto stage area 
# - Discard commit
# - Keep changes in staging area
# - Keep changes in working directory
git reset --soft [SHA] 
2. Mixed Reset (default)
# move pointer to SHA and goto SHA to stage area and last head , SHA goto working directory
# - Discard commit
# - Discard changes in staging area
# - Keep changes in working directory
git reset --mixed [SHA]
git reset [SHA]
3. Hard Reset
# goto last head to working direc then move pointer to SHA and go SHA to working direc and delete last head from working direc
# - Discard commit
# - Discard changes in staging area
# - Discard changes in working directory
git reset --hard [SHA]
# Log Details & Filter
git log
git log --stat  # commit with what changes in files
git log --oneline 
git log --oneline -3 #show three lines
git log --oneline --graph --all --decorate
git log --author="asd" --oneline
git log --grep="text" --oneline
git shortlog -n -s -e
git log --merges --oneline
git log --no-merges --oneline

# link_remote_repo => created in github
git remote add origin [link_remote_repo]

# Update remote repo
git remote update origin --prune

# Show Remote Name
git remote -v
git remote show origin

# Git fetch works with remote branches
git fetch <remote> # fetch all of the branches from the repository
git fetch <remote> <branch> # only fetch the specified branch
git fetch --all # A power move which fetches all registered remotes and their branches

# Get Last Update | fetch with merge
git pull -v
git pull origin

# To Push Local Repo To Remote Repo
git push [RemoteName] [BranchName]
git push origin master
git push -u origin master # push data -u for pull then push
git push origin master --force # to update commit


### Branches
# Show Branches
git branch
git branch -vv

# Create Branch
git branch [BranchName]

# Switch To Branch
git checkout [BranchName]

# Delete Branch
git branch -d BranchName

# Delete branch without confirm
git branch -D BranchName

# Create Branch And Switch To It
git checkout -b BranchName

# Move / Rename Branch
git branch -m oldBranch newBranch


### Set Configuration
git config --global user.email
git config --global user.email "asd@asd.com"
git config --global user.name "asd"

# Edit Configuration
git config --global --edit

# Create Public Key To Login Github Automatic
ssh-keygen -t rsa -b 4096 -C "Your GitHub Email Here" # Generate Key
cat ~/.ssh/id_rsa.pub # Get Key To Copy and Add SSH to Github Website
ssh -T git@github.com" # Test Key And Connection

# Alias
git config --global alias.br branch

## Ignoring Files and Directories
# create file .gitignore then add inside it extention of files ex. *.log permit some file added before !test.log

### Fork Repo
# Create Fork From Existing Project
1. make fork from project then if you want edit it after then commit to master or new branch then create pull request to admin 
2. wait admin to accept your pull request
3. after add branch create file and do some task then add files the commit 
 
# Merge
git merge BranchName

# To merge branch direct to origin without pull request
git checkout master
git merge BranchName # merge branch to master

# To merge branch to origin by pull request
git checkout [BranchName]
git push origin branchName # to create pull request
# merge branch to master then can delete branch and commit master or add master to origin
git merge BranchName

# pull request to remote with branch
git push origin BranchName

Advanced Git

for more about merge strategies in git https://www.geeksforgeeks.org/merge-strategies-in-git/

### Commit
git checkout -b tmp # when make changes and commit if u want to copy commit to another branch
git checkout master
git cherry-pick <sha1 of commit>  # commit with changes will set in master branch

### Reflog
git reflog
git reflog show temp


### Merge
# Merge Strategies
1. Fast Forward Merge (Recursive)
# fast forward merge is possible when there are no further commits in the receving branch after the commit where new branch was created
git merge [BranchName]
git merge -s recursive branch1 branch2
2. 3-Way Merge (Resolve)
# last commit from master and last commit from new branch | merge
git merge [BranchName]
git merge -s resolve branch1 branch2
3. Ours
# The output merge result is always that of the current branch `HEAD`
# used to combine history of similar feature branches
git merge -s ours branch1 branch2 branchN

# merge master to testbranch | copy all commits without any changes
# Records a merge but skips incoming changes
(testBranch)$ git merge --strategy=ours master
(testBranch)$ git merge -s ours master


### Merge Conflicts
git log --merge # show a list of commits that conflict between the merging branches

# Compare
git diff # show what you edit working directory
git diff --staged # show what you edit staged area

# Undo Merge
git merge --abort # return the branch to the state before the merge began
git checkout # undoing_ changes to files when git fails to start a merge
git reset --mixed  # reset can be used to undo changes to the working directory and staging area

# Fix Conflict
1. using VS Code
2. using automatic fix
	# --ours  => current branch | branch before conflict
	# --theirs => # The output merge result is always that of the incoming branch
	git checkout --ours / --theirs <file>  

keep both ours and theirs when conflict https://stackoverflow.com/questions/13263902/git-merge-keep-both

# Add a line to .gitattributes:
*.whatever merge=union
# Git Revert
git revert HEAD # revert commit changes from a specific commit
 
### Git Rebase
git rebase -i HEAD~3 # first 3 commit
# merge with rebase
(branch) git rebase master # move change on top of change master 
(master) git rebase branch # move path branch to master to be straight on master

### Rewriting Git History
# Amending commits
git commit --amend --no-edit

# Rewording commits [change commit name]
git rebase -i HEAD~2 # last 2 commit

# Deleting commits
git rebase -i HEAD~2 # change pick to drop

# Reordering commits
git rebase -i HEAD~2 # change ordering

# Squashing commits
git rebase -i HEAD~2 # change pick to squash|fixup

# Splitting commits
git rebase -i HEAD~2 # change pick to edit
git reset HEAD^ # unstage head 
git add file1
git commit -m "f1"
git add file2
git commit -m "f2"
git rebase --continue

### Stash

# Save Work To Stash to use later // hidding files from repo
git stash
# Get Work From Stash
git stash pop

# List Items in Stash
git stash list

# Save Work To Stash With Description
git stash save "comment"

# pop last index without remove from stash
git stash apply

# Get Specific Stash
git stash pop stash@{2}

# Delete Stash
git stash drop stash@{2}

#Show Lastest Added Stash Content
git stash show

# Show Specific Stash Content
git stash show stash@{2}

# Empty The Stash
git stash clear

### Tag and Release
git tag # show tags
git tag v1.0 # create tag
git push origin v1.0 # push tag to origin
git tag -a v2.0 -m "comment" # create tag version 2 or anything
git push origin v2.0  # push tag to origin
git tag -l v1.*  # list tags v1.* any
git tag -d v1.0  # remove tag local
git push origin --delete v1.0 # remove tag remote

After Finished tag create **Release**
#### Mirror existed repo to add your repos
git clone http://github.com/account/repo.git Cloned-repo
cd Cloned-repo
rm -rf .git
git init
git add .
git commit -m "first commit"
git remote add origin git@github.com:username/created-repo.git
git push -u origin master

for more about .gitattributes https://rehansaeed.com/gitattributes-best-practices/

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