Thursday, 17 January 2019

Git-Notes

To delete a branch

Local branch: git branch -d <local_branch> Remote branch: git push origin --delete <remote_branch>

To create new branch from existing branch

git checkout -b <new_branch_name> <source_branch_name>

To push my branch to remote server

git push -u origin <my_branch>

To view commit history logs

git reflog

To reset to particular commit log

git reset HEAD@{N}
*N is target number of commit log to reset to.

To show last git commit message on current checkout branch

git show --summary

To revert commits in certain branch to particular commit

git checkout <branch>
git reset --hard <commit-hash>
git push -f origin <branch>

To fetch all git branches

git pull --all

To create local branch to tracks remote branch

git checkout --track origin/john_branch
where --track is shorthand for git checkout -b [branch] [remotename]/[branch]

To reset local branch to match latest remote branch

git fetch origin
git reset --hard origin/master
git clean -fd

To rename a branch

git branch -m old_branch new_branch         # Rename branch locally    
git push origin :old_branch                 # Delete the old branch    
git push --set-upstream origin new_branch   # Push the new branch, set local branch to track the new remote

Successful git branching

Ref: https://nvie.com/posts/a-successful-git-branching-model/

Creating a feature branch

git checkout -b myfeature develop

Incorporating a finished feature on develop

git checkout develop
git merge --no-ff myfeature
.
.
.
git branch -d myfeature
git push origin develop
 
 

Using Git

Global Settings

Related Setup: https://gist.github.com/hofmannsven/6814278
Related Pro Tips: https://ochronus.com/git-tips-from-the-trenches/
Interactive Beginners Tutorial: http://try.github.io/

Reminder

Press minus + shift + s and return to chop/fold long lines!
Show folder content: ls -la

Notes

Do not put (external) dependencies in version control!

Setup

See where Git is located: which git
Get the version of Git: git --version
Create an alias (shortcut) for git status: git config --global alias.st status

Help

Help: git help

General

Initialize Git: git init
Get everything ready to commit: git add .
Get custom file ready to commit: git add index.html
Commit changes: git commit -m "Message"
Add and commit in one step: git commit -am "Message"
Remove files from Git: git rm index.html
Update all changes: git add -u
Remove file but do not track anymore: git rm --cached index.html
Move or rename files: git mv index.html dir/index_new.html
Undo modifications (restore files from latest commited version): git checkout -- index.html
Restore file from a custom commit (in current branch): git checkout 6eb715d -- index.html

Reset

Go back to commit: git revert 073791e7dd71b90daa853b2c5acc2c925f02dbc6
Soft reset (move HEAD only; neither staging nor working dir is changed): git reset --soft 073791e7dd71b90daa853b2c5acc2c925f02dbc6
Undo latest commit: git reset --soft HEAD~
Mixed reset (move HEAD and change staging to match repo; does not affect working dir): git reset --mixed 073791e7dd71b90daa853b2c5acc2c925f02dbc6
Hard reset (move HEAD and change staging dir and working dir to match repo): git reset --hard 073791e7dd71b90daa853b2c5acc2c925f02dbc6

Update & Delete

Test-Delete untracked files: git clean -n
Delete untracked files (not staging): git clean -f
Unstage (undo adds): git reset HEAD index.html
Commit to most recent commit: git commit --amend -m "Message"
Update most recent commit message: git commit --amend -m "New Message"

Branch

Show branches: git branch
Create branch: git branch branchname
Change to branch: git checkout branchname
Create and change to new branch: git checkout -b branchname
Rename branch: git branch -m branchname new_branchname or: git branch --move branchname new_branchname
Show all completely merged branches with current branch: git branch --merged
Delete merged branch (only possible if not HEAD): git branch -d branchname or: git branch --delete branchname
Delete not merged branch: git branch -D branch_to_delete

Merge

True merge (fast forward): git merge branchname
Merge to master (only if fast forward): git merge --ff-only branchname
Merge to master (force a new commit): git merge --no-ff branchname
Stop merge (in case of conflicts): git merge --abort
Stop merge (in case of conflicts): git reset --merge // prior to v1.7.4
Merge only one specific commit: git cherry-pick 073791e7

Stash

Put in stash: git stash save "Message"
Show stash: git stash list
Show stash stats: git stash show stash@{0}
Show stash changes: git stash show -p stash@{0}
Use custom stash item and drop it: git stash pop stash@{0}
Use custom stash item and do not drop it: git stash apply stash@{0}
Delete custom stash item: git stash drop stash@{0}
Delete complete stash: git stash clear

Gitignore & Gitkeep

About: https://help.github.com/articles/ignoring-files
Useful templates: https://github.com/github/gitignore
Add or edit gitignore: nano .gitignore
Track empty dir: touch dir/.gitkeep

Log

Show commits: git log
Show oneline-summary of commits: git log --oneline
Show oneline-summary of commits with full SHA-1: git log --format=oneline
Show oneline-summary of the last three commits: git log --oneline -3
Show only custom commits: git log --author="Sven" git log --grep="Message" git log --until=2013-01-01 git log --since=2013-01-01
Show only custom data of commit: git log --format=short git log --format=full git log --format=fuller git log --format=email git log --format=raw
Show changes: git log -p
Show every commit since special commit for custom file only: git log 6eb715d.. index.html
Show changes of every commit since special commit for custom file only: git log -p 6eb715d.. index.html
Show stats and summary of commits: git log --stat --summary
Show history of commits as graph: git log --graph
Show history of commits as graph-summary: git log --oneline --graph --all --decorate

Compare

Compare modified files: git diff
Compare modified files and highlight changes only: git diff --color-words index.html
Compare modified files within the staging area: git diff --staged
Compare branches: git diff master..branchname
Compare branches like above: git diff --color-words master..branchname^
Compare commits: git diff 6eb715d git diff 6eb715d..HEAD git diff 6eb715d..537a09f
Compare commits of file: git diff 6eb715d index.html git diff 6eb715d..537a09f index.html
Compare without caring about spaces: git diff -b 6eb715d..HEAD or: git diff --ignore-space-change 6eb715d..HEAD
Compare without caring about all spaces: git diff -w 6eb715d..HEAD or: git diff --ignore-all-space 6eb715d..HEAD
Useful comparings: git diff --stat --summary 6eb715d..HEAD
Blame: git blame -L10,+1 index.html

Releases & Version Tags

Show all released versions: git tag
Show all released versions with comments: git tag -l -n1
Create release version: git tag v1.0.0
Create release version with comment: git tag -a v1.0.0 -m 'Message'
Checkout a specific release version: git checkout v1.0.0

Collaborate

Show remote: git remote
Show remote details: git remote -v
Add remote origin from GitHub project: git remote add origin https://github.com/user/project.git
Add remote origin from existing empty project on server: git remote add origin ssh://root@123.123.123.123/path/to/repository/.git
Remove origin: git remote rm origin
Show remote branches: git branch -r
Show all branches: git branch -a
Compare: git diff origin/master..master
Push (set default with -u): git push -u origin master
Push to default: git push origin master
Fetch: git fetch origin
Fetch a custom branch: git fetch origin branchname:local_branchname
Pull: git pull
Pull specific branch: git pull origin branchname
Merge fetched commits: git merge origin/master
Clone to localhost: git clone https://github.com/user/project.git or: git clone ssh://user@domain.com/~/dir/.git
Clone to localhost folder: git clone https://github.com/user/project.git ~/dir/folder
Clone specific branch to localhost: git clone -b branchname https://github.com/user/project.git
Delete remote branch (push nothing): git push origin :branchname or: git push origin --delete branchname

Archive

Create a zip-archive: git archive --format zip --output filename.zip master
Export/write custom log to a file: git log --author=sven --all > log.txt

Troubleshooting

Ignore files that have already been committed to a Git repository: http://stackoverflow.com/a/1139797/1815847

Security

Hide Git on the web via .htaccess: RedirectMatch 404 /\.git (more info here: http://stackoverflow.com/a/17916515/1815847)

Large File Storage

Website: https://git-lfs.github.com/
Install: brew install git-lfs
Track *.psd files: git lfs track "*.psd" (init, add, commit and push as written above)
 

How to delete a commit from git


Using Rebase this will allow you to remove one or more consecutive commits

Example git log

Number
Hash
Commit Message
Author
1
2c6a45b
(HEAD) Adding public method to access protected method
Tom
2
ae45fab
Updates to database interface
Contractor 1
3
77b9b82
Improving database interface
Contractor 2
4
3c9093c
Merged develop branch into master
Tom
5
b3d92c5
Adding new Event CMS Module
Paul
6
7feddbb
Adding CMS class and files
Tom
7
a809379
Adding project to Git
Tom

Using Rebase

Using the git log above we want to remove the following commits; 2 & 3 (ae45fab & 77b9b82). As they are consecutive commits we can use rebase.

git rebase --onto <branch name>~<first commit number to remove> <branch name>~<first commit to be kept> <branch name>

e.g to remove commits 2 & 3 above

git rebase --onto repair~3 repair~1 repair


Delete Commit History in Git Repository

 

Follow the below steps to complete this task.
Warning: This will remove your old commit history completely, You can’t recover it again.
  • Create Orphan Branch – Create a new orphan branch in git repository. The newly created branch will not show in ‘git branch’ command.
    $ git checkout --orphan temp_branch
    
  • Add Files to Branch – Now add all files to newly created branch and commit them using following commands.
    $ git add -A
    $ git commit -am "the first commit"
    
  • Delete master Branch – Now you can delete the master branch from your git repository.
    $ git branch -D master
    
  • Rename Current Branch – After deleting the master branch, let’s rename newly created branch name to master.
    $ git branch -m master
    
  • Push Changes – You have completed the changes to your local git repository. Finally, push your changes to remote (Github) repository forcefully.
    $ git push -f origin master
    

 

Short Form of commands:

cd Repo 
rm -rf .git
git init
git remote add origin git@github.com:JohnDoe/foobar.git
git remote -v
git add --all
git commit -am "Initial commit"
git push -f origin master
 
 

 

First Method

Deleting the .git folder may cause problems in our git repository. If we want to delete all of our commits history, but keep the code in its current state, try this:

# Check out to a temporary branch:
git checkout --orphan TEMP_BRANCH

# Add all the files:
git add -A

# Commit the changes:
git commit -am "Initial commit"

# Delete the old branch:
git branch -D master

# Rename the temporary branch to master:
git branch -m master

# Finally, force update to our repository:
git push -f origin master

This will not keep our old commits history around. But if this doesn't work, try the next method below.

Second Method

# Clone the project, e.g. `myproject` is my project repository:
git clone https://github/heiswayi/myproject.git

# Since all of the commits history are in the `.git` folder, we have to remove it:
cd myproject

# And delete the `.git` folder:
git rm -rf .git

# Now, re-initialize the repository:
git init
git remote add origin https://github.com/heiswayi/myproject.git
git remote -v

# Add all the files and commit the changes:
git add --all
git commit -am "Initial commit"

# Force push update to the master branch of our project repository:
git push -f origin master

NOTE: You might need to provide the credentials for your GitHub account.