Git

Useful commands

  • Reset repo condition no matter it is being merged, cherry-picked, etc… locally
git reset --hard HEAD

What’s Git LFS?

For projects containing large files, particularly large files that are modified regularly, this initial clone can take a huge amount of time, as every version of every file has to be downloaded by the client

Git LFS (Large File Storage) is a Git extension developed by Atlassian, GitHub, and a few other open source contributors, that reduces the impact of large files in your repository by downloading the relevant versions of them lazily

Specifically, large files are downloaded during the checkout process rather than during cloning or fetching.

Git LFS does this by replacing large files in your repository with tiny pointer files.

Reference: git-lfs

Git configuration

How to disable git hook pre commit?

Open .git/hooks/pre-commit and comment out anything you like

How to add an exemption folder into gitignore?

/build/*
!/build/your-exemption-folder

Git use cases

How to cherry-pick a commit you deleted?

git reflog
git cherry-pick [hash_code]

How to commit all files by CLI?

git add -A && git commit -m "Your Message"

How to create a repository

git init --bare

How to reset all local changes?

git reset --hard HEAD

How to return current branch name only?

git rev-parse --abbrev-ref HEAD

How to revert all changes

[js]git checkout .[/js]

How to temporarily “stash” current changes?

[js]$ git stash
$ git pull
$ git stash pop[/js]

How to edit comment of the last commit?

[js]git commit –amend -m “New and correct message”[/js]

How to list up all branches?

[js]git branch -a[/js]

How to find a branch name?

[js]git branch –all | grep “Debug”[/js]
[js]// case insensitive
git branch –all | grep -i “Debug”
[/js]

How to find current branch name?

[js]git branch | grep \*[/js]
or
[js]git rev-parse –abbrev-ref HEAD[/js]

Git Client for Mac

Use Source Tree of Atlassian

How to get new branch information without merging?

[js]
git fetch //safe for any cases
[/js]

How to get new branch information and also merge latest source from origin to local?

[js]
git pull
[/js]

How to ignore a branch when pushing?

To also prevent pushing by explicit, set the branch..remote to some some nonexistent remote.
[js]
.git\config

[branch “testing_react”]
remote = nonexistent_remote
[/js]

How to reset to previous commit?

[js]
git reset –hard commit_sha
//Be aware that any modified and uncommitted/unstashed files will be reset to their unmodified state.
[/js]

How to undo a Git merge that hasn’t been pushed yet?

[js]
git reset –hard ORIG_HEAD
//ORIG_HEAD will point to a commit directly before merge has occurred, so you don’t have to hunt for it yourself.
[/js]

How to merge a single commit into a branch?

Use cherry-pick

How to clone and create a branch from a tag?

After the clone, you can list the tags with $ git tag -l and then checkout a specific tag:
[js]
$ git checkout tags/
[/js]
Even better, checkout and create a branch (otherwise you will be on a branch named after the revision number of tag):
[js]
$ git checkout tags/ -b
[/js]

How to grep commit comment

[js]

[/js]

How to grep commit comment

git log --all --grep='some comment'

How to list out all unmerged branch

git branch --merged master
git branch --no-merged master
git branch --merged // show you branches which have been merged into the current HEAD

Rename 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

How to undo a merged commit

You need to reset the head to the commit just before your current head

git reset --hard <commit_before_merge>

How to know when branch is created

git show $(git merge-base parent_branch_name branch_name)

this only works if your-branch hasn’t been merged with parent_branch_name

How to remove commits that you have pushed?

git reset --hard [hash_number]    # previous commit that you want to HEAD
git push origin [my_branch] -f    # forced update origin branch like local

Notes:

  • “git reset –hard” throws away all uncommitted changes
  • “git reset –soft” not throws away all uncommitted changes
  • “git push origin [your_branch] -f” can be dangerous if [my_branch] was already fetched by others in their own repo
  • If your remote repo (‘origin’) has its config set with “receive.denyNonFastForwards true”, it will deny any non fast-forward push (even when forced)

How to split a commit into separate branch?

master ───●──●──●──●──●──●──●──●──●──●
              \                    
               \                   
        feature ●──●──●──●──●──●──●
                         ▲         
                         │         
                    split here   

to

master ───●──●──●──●──●──●──●──●──●──●   
              \                       
               \                      
        feature ●──●──●──●            
                          \           
                           \          
              feature-test  ●──●──●
git checkout feature
git checkout -b feature-test

git checkout feature
git reset --hard <sha1 split here>

git push --force
git push origin feature-test

How to edit comment of latest commit?

git commit --amend
git push origin master --force

https://gist.github.com/nepsilon/156387acf9e1e72d48fa35c4fabef0b4

How to create an empty repository

#!/bin/bash
mkdir repo
cd repo
git init --bare
cd ..
mkdir clone
cd clone
git clone ../repo
mv repo git
mv git ../git
cd ..
rm -rf clone

Be the first to comment

Leave a Reply

Your email address will not be published.


*