Popular Git Commands
This blog briefly goes through some essential git commands that may prove handy.
- Downloading Git for Windows
- Pushing from a Local Repository to GitHub
- Adding, Committing and Pushing Files
- Pushing from a Remote Repository to a Local Folder
Let’s start with the technicalities: Git is a version control system that tracks source code modifications in software development, while GitHub is a collaboration and version control platform for storing and managing code. More on the differences here. GitHub is a version control tool used by programmers worldwide; other variations of this service are GitLab, Bitbucket, among others.
For the budding data scientist, GitHub can also be beneficial in showcasing your projects (scripts and notebooks) and used to host your portfolio website.
The following is not an exhaustive list of Git commands, but rather what I use regularly. The steps below lived on a text file on my local computer that I consulted often when I first started using Git.
Downloading Git for Windows
To download Git, go to the official git site and follow the prompts. Select the defaults except you know better.
Pushing from a Local Repository to GitHub
- On your local desktop or folder of interest, create a new folder for your project and copy the address of the folder.
- Open up Command Line and navigate to the folder using:
cd C:\Users\Ibiene\OneDrive\blog-test
Initialize git:
git init
- On GitHub, create a new repository, name the repository but DO NOT add a readme file.
- Copy the address of the repository. This address will be pasted in the next step.
Back to Command Line, run the command below to connect your local folder to the remote git repository:
git remote add origin https://github.com/ibiene-ds/blog-test.git
Replace the example .git link with Ctrl+V - to paste the repository address you copied in the previous step.To verify that you have a successful connection:
git remote -v
- If an error comes up such as remote origin already exists, try the following steps in Command Line:
- Remove remote repo:
git remote rm origin
- Repeat
remote add...
&git remote -v
commands.
- Remove remote repo:
Usually, I get an error when I mistakenly add a README file at the beginning (when I created the repo). So my quick fix is deleting and re-creating the repository without the README file.
- Navigate to the folder and you should see a .git folder (looks transparent). You can also see this by using the command below in Command Line:
dir /ah
This shows all files, including hidden files and hidden directories.
If you are using Gitbash, the command below achieves the same purpose:ls -a
Adding, Committing and Pushing Files
To add all files to your local git repository:
git add .
The ‘.’ means All. Alternatively, you can use:
git add -A
To add a single file, use:
git add <file-name>
To add updates to a file that has already been committed, use:
git add -u
Next, commit:
git commit -m "added image dimensions"
“-m” means message and implies that you will add a commit message such as “added image dimensions”.
For your message, add a helpful comment about the changes you made so you can track the changes if you need to go back to a previous version.- Push the contents of your local repository to the remote repository:
git push -u origin main
OR
git push origin master
- The choice of main or master depends on your initial choices when you first installed git, but they both refer to your main branch.
- The message after your first commit should tell you what your main branch is called. For instance, mine is called ‘master’ so pushing to ‘main’ will generate errors.
- To check status:
git status
Pushing from a Remote Repository to a Local Folder
- Create a new repository on GitHub, name the repository and add a README file.
- Enter into the new repository.
- Using the green button called Code - copy the location of the repo
In Command Line, navigate to your working folder and clone the repo into your local computer using the command:
git clone https://github.com/ibiene-ds/myrepo.git
Go to your cloned repo:
cd path/to/new/folder
Now, you can change files on your local computer, save and then push them to GitHub when satisfied.