Introduction to Git
- Version control is like an unlimited ‘undo’.
- Version control also allows many people to work in parallel.
Why use Git?
Why using Git will make you a better researcher:
- You know you can always go back to a working version of your code
- You will have a way of showcasing your projects
- You will be able to distribute your code to others
- You will be able to modify other peoples’ code and make contributions to it
- You will have a digital lab notebook
Fundamental concepts
- Commits are snapshots of a repository.
- We can have a copy of our repository in a different location, called a remote. We can send (“push”) and receive (“pull”) changes from the remote to interact with others’ work, and also to back up or update our local copy.
- The staging area is where we keep track of the changes that we are going to commit, that is, which are going to be written in the repository history.
- Branches are isolated timelines of a repository, where people can work in parallel.
Your first Git repository
-
git initinitializes a repository. - Git stores all of its repository data in the
.gitdirectory.
Tracking changes
We learned several new commands. Let’s take note of them:
-
git initinitialises a Git repository, which is contained within the hidden.gitdirectory. -
git statusshows the current state of the repository, such as which files have been changed. -
git addadds a file to the staging area, where we gather all of the changes that will be committed. -
git commitwrites the changes to history, permanently recording them to the repository. -
git logshows the list of commits in the repository. -
git diffshows the changes between the last commit and the current repository, showing how files have been changed.
Remotes: pushing and pulling
- The
git pushcommand pushes local changes to the GitHub remote. Before running it, we must set our remote to the correct URL. - The
git pullcommand pulls remote changes to our local repository. We need to run it to sync our local repository to the newest updates on the remote. - The
git remotecommand is used to manage remotes, such as setting the GitHub URL, to view the configured remotes, and other actions.
Cloning a repository
-
git clonecopies a remote repository to our local repository. -
git checkoutallows us to move between commits – back and forth through a repository’s timeline. -
HEADindicates the current state of the repository.HEAD~1,HEAD~2refer to one or two commits before the current - We cannot push to a remote repository if we do not have access to it.
Forking a repository
- Forking a repository creates a new remote that we have control over.
- We can update the remote on our local repository using
git remote set-url <REMOTE_NAME> <REMOTE_URL>. - Pull requests are a way of collaborating that allows other people to merge our changes into their repository.
- Conflicts are when two commits modify the same parts of a file. We must resolve conflicts before merging them. This can be done by creating a new commit.
- Conflicts are denoted by
<<<,>>>and===characters around the conflicting lines.
Tips and best practices
- There are a number of strategies to incorporate Git into your day-to-day work.
- The learning curve can seem steep at first, but with time you will understand how valuable Git is and it will become easier and easier to use.
- GitHub offers numerous features that make adopting Git easier, and enable you to showcase your projects.
- There are a few best practices which will dramatically improve your efficiency with Git.