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.
Your first Git repository
-
git init
initializes a repository. - Git stores all of its repository data in the
.git
directory.
Tracking changes
We learned several new commands. Let’s take note of them:
-
git init
initialises a Git repository, which is contained within the hidden.git
directory. -
git status
shows the current state of the repository, such as which files have been changed. -
git add
adds a file to the staging area, where we gather all of the changes that will be committed. -
git commit
writes the changes to history, permanently recording them to the repository. -
git log
shows the list of commits in the repository. -
git diff
shows the changes between the last commit and the current repository, showing how files have been changed.
Remotes: pushing and pulling
- The
git push
command pushes local changes to the GitHub remote. Before running it, we must set our remote to the correct URL. - The
git pull
command 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 remote
command is used to manage remotes, such as setting the GitHub URL, to view the configured remotes, and other actions.
Cloning a repository
-
git clone
copies a remote repository to our local repository. - There are multiple ways to revert to previous changes, one of them
is
git reset
. -
HEAD
indicates the current state of the repository. - 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.