What is git cherry-pick & .gitignore file

Amit Prajapati
MindOrks
Published in
7 min readSep 20, 2019

In this blog, we will understand the git commands like .gitignore and git cherry-pick.

Today’s Motivation

“You must find the place inside yourself where nothing is impossible.”

So, Let’s get started… 👩‍💻

What is the use of git ignore file?

Sometimes, our project not only consists of the code that we created. There are some files that are generated by our IDE or our framework that aren’t actually needed. These files or folders sometimes also may generate errors when copied to other PC, so that’s why we need to ignore those unnecessary files or folders.

Ignored files are usually build artifacts and machine-generated files that can be derived from your repository source or should otherwise not be committed. Some common examples are:

  • dependency caches, such as contents of /node_modules or /packages
  • compiled code, such as .o, .pyc, and .class files
  • build output directories, such as /bin, /out, or /target
  • files generated at runtime, such as .log, .lock, or .tmp
  • hidden system files, such as .DS_Store or Thumbs.db
  • personal IDE config files, such as .idea/workspace.xml

To know more about which file you can ignore in your project click on the below link. 👇

The entries in this file can also follow a matching pattern.

  • * is used as a wildcard match.
  • / is used to ignore pathnames relative to the .gitignore file
  • # is used to add comments to a .gitignore file

This is an example of how our .gitignore file will look like:

# Ignore Mac system files
.DS_Store
# Ignore node_modules folder
node_modules
# Ignore all text files
.txt
# Ignore files related to API keys
.env
# Ignore folder named 'output'
output/

This is the image of our remote repository. 👇

Remote Repository

Let’s have a look at our local repository as well.

Local Repository

The command dir is used to view the files and directories of your working directory.

The command ls -a is used to view the hidden files such as .git and .gitignore files are present.

for e.g: Let’s say, you create another file named ‘payment.txt’ but you didn’t want git to track this file. So, you can put this file in the .gitignore file. We use the touch command to create this file.

touch .gitiginore

In the above image, you can see the .gitignore file and payment.txt file are there. Now, let’s try to run the git status command and see what happens?

As you see in the above image, git doesn’t track “payment.txt” because it’s under .gitignore file. Git ignores the files which are present in the .gitignore file after that you will add the .gitignore file to the staging area and you commit and push it to GitHub, but you won’t able to see the payment.txt file in your remote repository.

Now, let’s have a look at our remote repository.

In the above image, you see the .gitignore file is added but “payment.txt” is not. Now, let’s have a look at what’s inside .gitignore file.

Inside the .gitignore file, we added the name of the file which wants to ignore in our case, it is the “payment.txt”.

What is git cherry-pick?

When you are working with a team of developers on a medium to a large-sized project, managing the changes between the number of git branches can become a complex task. Sometimes you don’t want to merge a whole branch into another, and only need to pick one or two specific commits. This process is called ‘cherry-picking’.

What git cherry-pick does, basically, is take a commit from somewhere else, and “play it back” wherever you are right now. Because this introduces the same change with a different parent, Git builds a new commit with a different ID.

Let’s look at this example.

If you were at node H in this graph, and you typed git cherry-pick E (yes, you’d actually type part or all of the SHA (Secure Hash Algorithm) or hash key) for the commit, but for simplicity’s sake, I’ll just use the labels that are already here), you’d wind up with a copy of commit E — let’s call it “E prime” or E’ — that pointed to H as its parent, like so:

Or, if you typed something like git cherry-pick C D E , you’d wind up with this when you were done.

The important thing to notice here is that Git has copied changes made in one place, and replayed them somewhere else.

When should we use Cherry Picking?

I will try to explain this through a scenario that will make it easier to understand.

for e.g: Let’s create a new branch called new-features.

Let’s say you are working on the new-features branch and you made some changes in the file called courses.txt and commit it and push it into the new-features branch.

Here, we made some changes to the courses.txt and push it on the new-features branch. Now, you have already made changes in the courses.txt but you want to move the courses.txt file changes into the master branch. We can do with the help of git merge command, but we don’t want to merge the whole new-features branch in it. We just want the specific commit which was performed in the courses.txt and add this commit on the master branch.

From the “new-features” branch run git log --oneline to get a better log of our commits history. Note that the commit hash is what we need to start the cherry-picking.

List of commits performed in the new-features branch.

Now, check-out the branch where you want to cherry-pick the specific commits. In this case master branch.

git checkout master

The above image shows that we need to checkout to the master branch then perform git log --oneline command to display the commits in the master branch.

Now we can cherry-pick from new-features branch:

git cherry-pick commit-id

This will cherry-pick the commit with hash ‘d545959’ and add it as a new commit on the master branch.

Note: It will have a new (and different) commit ID in the master branch.

If you want to cherry-pick more than one commit in one go, you can add their commit IDs separated by a space.

e.g. git cherry-pick d467740 de906d4

As you see in the image above, we successfully added the new-features commit in the master branch.

If the cherry-picking gets halted because of conflicts, resolve them and git cherry-pick --continue

  • --continue: This can be used to continue after resolving conflicts in a failed cherry-pick or revert.

If you want to bail off this step out altogether, just type: git cherry-pick --abort

  • --abort : Cancel the operation and return to the pre-sequence state.

After all, this is done, you can simply push the new commits to the upstream repo (e.g. origin) and get on with your day.

Hopefully, this blog should have helped you in understanding the .gitignore file and git cherry-pick command.

If you really enjoyed reading this blog, give me a clap and share it :-)

Thank you.

--

--