Git is not that hard to learn…

What is Github?

GitHub is a code hosting platform for version control and collaboration. It lets you and others work together on projects from anywhere.

What is Git?

A version control system, or VCS, tracks the history of changes as people and teams collaborate on projects together. As developers make changes to the project, any earlier version of the project can be recovered at any time.

In the beginning, you may not need to share your project’s codes for collaboration or you may not need to track your code’s versions. Kaggle-kinda platforms can be enough to share your code to just let people see.

But…

After a while, I mean when you start to create big projects that you might need to track and wait for other people to add new things, or when you have a new job with multiple teammates, you’ll probably need something like Git.

Git makes it possible to let you see what changes have been made by who and when. Makes it possible that multiple people to work together without crushing changes on the same project.

Developers can review project history to find out:

  • Which changes were made?
  • Who made the changes?
  • When were the changes made?
  • Why were changes needed?

And Github is just a platform that makes other people see your project’s codes, who’s collaborating and let them use it for their own.

Now, I think it’s time to learn how to use Git. Let’s get starteeed 😎

Step 0: Installation

You can download Git from here. You can choose the operating system.

For Windows systems, Git Bash will be downloaded with Git. You can use Git Bash for this course. But for my case I’ll use terminal because my operating system is MacOS.

After installation type this command in your terminal or Git Bash:

git --version

Output must be something like this:

git version 2.34.1

🔗 If you have downloaded Git before your version might be older, you can upgrade it.

You can see git commands with this command:

git --help

Step 1: Git Setup

We need to introduce ourselves to Git first. We should let it know which account is making those changes, creating new projects or cloning repositories.

git config --global user.name "YourGithubUserName"
git config --global user.mail "YourEmailAdressForGithub"

To see our credentials for Git:

git config -l

Step 2: Git Init

In you terminal you should go to the folder that you want to initialize. If you want to start controlling your project directory with Git, you should initialize it first.

git init .

This creates a new subdirectory named .git that contains all of your necessary repository files — a Git repository skeleton.

Nothing in your project is tracked yet. If you don’t have any file in your project folder it’s fine for now. After you add necessary files, you can follow the next steps.

ls -a #list hidden files

The output must be like this:

.    ..    .git

You remove .git file like this:

rm -rf .git

Basically project folder is no longer a git repository. You should initialize this folder again.

Step 3: Git Add

After you added your files inside the project folder, you should let git to track them. With this command you can specify the files you want to track:

git add .

The dot in here means all the files in the folder. If you want a specific file you can use file’s name instead of dot.

You can check the status of your files with this command:

git status

Output will show you your branch, untracked and tracked files.

If you would like to remove added file:

git rm --cached <filename>

If you would like to remove all added files:

git rm -r --cached .

📌 How would git work when we go into a folder inside the project folder?

In this case git will only work on files inside that folder.

git add -A 
# adding all the files include outside of the current folder, in project folder

Step 4: Git Commit

Creates a commit, which is like a snapshot of your repository. These commits are snapshots of your entire repository at specific times. The message in your commit must be clear about what you’ve changed in the file.

git commit -m "YourCommitMessage"

If you want to see commit logs or a specific commit, type this commands:

git log
git log --oneline # clean log
git show <CommitHash>

If you want to see difference between commits:

git diff

Commit messages are important for you or your teammate to understand what you’ve changed in the code. So if you would have to change your commit message, you should use this command:

git commit --amend -m "NewCommitMessage"

With this command you’re not making a new commit, you’re changing the existing last one with a new commit message.

We created a projects and let git to track it. So it’s in our local computer but anything can happen. We might delete it or our system can reset itself. We should put them in a remote repository too. That’s where Github comes in to equation. So people can see your projects, and it’s changes now and also you can find your project when you lost your local files. We have an existing repo on our local machines, we just need to upload them to Github.

Months ago Github release a new feature that requiring you to get your personal access token to make commit changes in a repo.

Go to Settings > Developer settings > Personal access tokens and click the ‘Generate new token’ option in the up-right. This will welcome you. Follow the steps and you’ll got your personal access token.

You can use it when you’ve asked to give your credentials to Git rather using your password.

Step 5: Git Push

You created a repo, put it on Github and you made changes in your local files and you want to update your Github files. You should use this command:

git push

This would be enough but when you work in another branch you might need a longer command. We’ll get to that later.

Step 6: Git Pull

If someone else has made a change in your repo and you cannot see it in your local project. You have to pull changes.

git pull

Step 7: Branches

One day multiple people will work on a same project. We cannot expect everyone to work on main branch. All of the changes can become complicated to understand and handle. That’s why there’s branches.

Create a new branch:

git branch <BranchName> # create branch
git checkout <BranchName> # switch to that branch
git checkout -b <BranchName>

With the last command you can create and go to that branch at the same time.

git branch -r # remote branches
git branch -a # all branches including remotes
git checkout - # switch to the branch that before this one

Let’s assume you or someone else made a change in a new branch other than main branch. You won’t be able to see those changes in main branch and Github. The person who made those changes should push their codes to main branch. But of course owner of the project should review changes first and then should approve merge with main branch.

To push commit from another branch:

git push -u origin <CurrentBranchName>

After this command you won’t see changes in main branch. The new branch will have gone to Github for review and approval from the owner.

Step 8: Deleting a Branch

git checkout -b will-be-deleted
git branch -d will-be-deleted

To push a new branch:

git push --set-upstream origin <BranchName>

Step 9: Git Pull Requests

A pull request is an event in Git where a contributor asks a maintainer of a Git repository to review code they want to merge into a project.

You’ve change something but of course it can’t merge with main branch just like that. First of all the owner of the project/repo should give the permission to merge with the main code. For this, you open a pull request and let them know you want to change something with an explanatory commit message. If your request got accepted, then your code can be merged with the main code by owner of the repo.

Step 10: Conflicts

Conflicts can happen when more than one person make changes in the same branch. If there is a conflict you can’t push your code to main. Git will raise an error about conflicts that you should solve first. It’s easy. In VS Code you can see conflicts pretty clear. All you have to do is delete the changes you don’t need and keep the changes you want. Then you can push your code.

After you resolved conflicts, you can add, commit and then push your code.

So, we’ve covered pretty much everything you need to know for start and will use them in your job or projects. All you have to do is to create a test repo and do what we’ve covered so far. Commit and uncommit changes, see the status, push and pull your codes.

This was just a resource I prepared, as I have given this course online before. So I was thinking why not share it with everyone. Maybe I’ll add a YouTube video link here soon somewhere 😎 Hope you find it instructive. If something you want me to know, let me know in the comments 😉

You SHOULD check this out.

A great documentation is the best thing evaaa 🥳 🤩 The creators give you the free access of something really useful and gorgeous is the great thing about open source community. Use it as you wish 😇

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Güldeniz Bektaş

Güldeniz Bektaş

Space 🌌 | Machine Learning 🌠 | Deep Learning 🌈