Git Table of Content




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Git cherry-pick

The cherry-pick command is used to apply a commit from one branch to another branch.

Let’s say you have a feature branch that you are working on that have two stories. You finished the two stories but only need to push the first story to master. You have one commit for story number one and another commit for story number two. Note here that you can’t use merge because merge will bring all the changes(story one and two together). We don’t want that we only need the first story code. Here you can cherry-pick the commit for story number one to master.

Steps
1. Find commit id of the commit you need to cherry-pick
2. Checkout the branch you want to apply the commit to
3. git cherry-pick commit-id

git checkout master
git cherry-pick 1232349F

Here is another example. Let’s say you are at branch (F-G-H) and you want the change that’s made in E. To do this you need to cherry-pick E. Note that these notes are commit ids.

git cherry-pick E

If you want to cherry-pick more than one commits then do this

git cherry-pick commit-id-1 commit-id-2 commit-id-3

Cherry pick all commits of a specific branch

When you want to cherry pick all commits of a specific branch but not commits merged or cherry-picked into it. You can just cherry pick the whole branch. Maybe there are so many commits that it does not make sense to specify each commit.

Let say you have a branch call color, css, and grid. You working on the css branch but have changes from color branch that you don’t want to push to the grid branch. What you need to do is cherry pick all commits from the css branch to the grid branch. Here is how you do that.

git checkout css
// make changes
// commit and push(abc)


git merge color
// merged 1 commit(cde) from color to css(the working branch)

// make changes again
// commit and push(fhg)

// Cherry pick all of css commits(without color commits) to grid
git checkout grid

git cherry-pick css
// you have commits abc and fhg but not cde.

 

November 5, 2020

Git log

Shows commit logs.

git log is intended for creating release announcements. It groups each commit by author and displays the first line of each commit message. This is an easy way to see who’s been working on what.

git log

 

Show insertions and deletions to each file

The --stat option displays the number of insertions and deletions to each file altered by each commit (note that modifying a line is represented as 1 insertion and 1 deletion). This is useful when you want a brief summary of the changes introduced by each commit. 

git log --stat

 

Show actual changes

If you want to see the actual changes introduced by each commit, you can pass the -p option to git log. This command is very useful due to the fact that it shows details of what and when things changed over time.

git log -p

 

Show history

The --graph option draws an ASCII graph representing the branch structure of the commit history. This is commonly used in conjunction with the --oneline and --decorate commands to make it easier to see which commit belongs to which branch.

git log --graph --oneline --decorate

 

Show certain number of commits

git log -n

// show last 5 commits
git log -5

 

Show commits by author

The --author  displays commits per specific author

git log --author="folaulau"

 

Get commit differences

This command is particularly useful when you use branch references as the parameters. It’s a simple way to show the differences between 2 branches.

git log master..another-branch

// show commits that in feature but not in master
git log master..feature

Note that if you switch the order of the range (feature..master), you will get all of the commits in master, but not in feature. If git log outputs commits for both versions, this tells you that your history has diverged.

 

August 5, 2020

Git branch

List all branches within a repository

git branch 

//or 

git branch --list

List all of remote branches

git branch -a

Create a local branch

git branch {branch-name}

Example: create a branch name test-100

git branch test-100

Check for the new created branch

folaukaveinga@Folaus-MacBook-Pro-3 demo % git branch
  develop
  master
  test-100

Push new local branch to remote

git push --set-upstream origin {branch-name}

Delete local branch

Use -d for safe delete. This means that if there are changes or commits in the branch that have not been commited or pushed up to remote, delete won’t work.

git branch -d {branch-name}

Use -D to force delete the specified branch, even if it has unmerged changes.

git branch -D {branch-name}

Delete a remote branch

git push origin --delete {branch-name}

 

Rename a branch locally

if you want to rename the branch remotely, Use push origin to reflect the change remotely.

git branch -m {new-branch-name}

Pull a remote branch to local

When you want to pull a remote branch to your local.

git pull origin {remote-branch-name}

// and

git checkout {remote-branch-name}

 

November 2, 2019

Git Pull Request

A pull request is a request asking your upstream project to pull changes into their tree. The request, printed to the standard output, begins with the branch description, summarizes the changes and indicates from where they can be pulled.

The upstream project is expected to have the commit named by <start> and the output asks it to integrate the changes you made since that commit, up to the commit named by <end>, by visiting the repository named by <url>.

git request-pull [-p] <start> <url> [<end>]

 

October 19, 2019

Git restore deleted commit

Sometime you commit something but then accidentally deleted it and then you would like to bring it back or continue working on it. I had this happened to me and it was frustrating to redo everything I deleted. But then I found out there is a way to recover deleted commit.

Well, you want to reset to that commit so you can continue working on it. Using git reflog, which will show you logs on what you have committed, will show you your deleted commit hash which then you can use to reset to.

git reflog

git reset –hard f52ee7b

June 7, 2019