I guess everyone faces some problems in git commands or Github in the first years of work-life. Therefore, I took some notes for myself and I would like to share them with you. I hope it can guide you too.
What will you read below...
- What is Github?
- What is Git?
- How are Git and GitHub related?
- How to create a GitHub account?
- What is a repository?
- How to create a repository on GitHub?
- Differences between Local & Remote Repositories
- Some basic Git commands
- Why did we use different branches?
- How do we work together thanks to branches?
What Is GitHub?
GitHub is a code hosting platform for revision control and collaboration. It lets you and others work together on projects from anywhere.
What is Git?
Git is a free and open DVCS designed to handle computer programming projects with speed and efficiency. It is easy to learn and has a very high performance.
Git runs locally. Shortly, It allows you to use the Version Control System on your laptop. It means that you will have in your computer a Version Control System, to recover all code, see what changes you have done, etc.
How are Git and GitHub related?
Git allows you to control your code in your local machine, while GitHub will enable you to collaborate with your teammates by pushing and pulling the code you and the others have been working on.
How to Create a GitHub account?
- Go to Github’s website: https://github.com/
- Fill in the blank according to your information and click the sign-up button. (username, e-mail, and password)
What is a repository?
The most fundamental component of version control is the so-called repository. The repository is a database that records additional information about your files (who made the changes, when and comments entered about the article) as a separate version. Git keeps all this information as a hidden folder in the file system as a series of folders within the file .git folder.
How to create a repository on GitHub?
You can use two methods to create your own share of the repository I mentioned.
If you have a project that is not under version control yet * add your project to version control with all its folders and files with git init command.
mkdir [project name] // creating folder on Terminal
cd [project name] // go to ‘project name’ folder
git init // creates a new Git repository
git add . // adds a change in the working directory to the staging area
git commit -m ‘initial commit’
git remote add origin https://github.com/ceydaulubas/first-repository.git
git push -u origin master
- Go to the GitHub web page.
- Open your account.
- There are 2 buttons for creating a new repository.
- After clicking one of the buttons, you should give the repository name and you can select the public or private repository. And finally, if you want to explain your code on the Read me part, you should select Add a README file. (But I didn’t select this in the first method, I will show it to you in a second way.)
- In the final step, follow the instructions.
An Important Tip !!!
While you are following this instruction, pay attention to the branch name. If you change your branch with a “ git branch -m main” comment, you should push your code to the main branch.
Second Method: (Cloning)
You can download the project with the clone command in its continuation under version control in a Git menu in your remote or company network.
- Follow the same steps in first method ( create a new repository on GitHub, give the name for repository and click ”Add a README file” button as a different from the first method.)
- Click the green Code button and copy the link you see below.
- Write git clone and paste the link you copied on the terminal like below)
Local & Remote Repositories
Local repository is the .git folder under your project folder on your computer. Only you can work on this repository and the changes are saved on your local disk.
Remote repositories are usually located on a remote server and consist of a .git folder on that server. When it comes to teamwork, the people on the team share their changes on this remote repository.
Some Basic Git commands
git init: Creates a new Git repository. It can be used to convert an existing, unversioned project to a Git repository or initialize a new, empty repository. Most other Git commands are not available outside of an initialized repository, so this is usually the first command you’ll run in a new project.
git add: Adds a change in the working directory to the staging area. It tells Git that you want to include updates to a particular file in the next commit. However, git add doesn’t really affect the repository in any significant way — changes are not actually recorded until you run git commit.
git commit: Save your changes to the local repository.
git status: Displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven’t, and which files aren’t being tracked by Git.
git log: Shows the status output about the project history committed.
git clone: Creates a copy of a specific repository or branch within a repository.
git push: is used to upload local repository content to a remote repository.
git pull: is used to fetch and download content from a remote repository and immediately update the local repository to match that content.
git fetch: downloads commits, files, and refs from a remote repository into your local repo. Fetching is what you do when you want to see what everybody else has been working on. It’s similar to svn update in that it lets you see how the central history has progressed, but it doesn’t force you to actually merge the changes into your repository. Git isolates fetched content from existing local content; it has absolutely no effect on your local development work. Fetched content has to be explicitly checked out using the git checkout command. This makes fetching a safe way to review commits before integrating them with your local repository.
The use of branches in Git is not optional, even if you are not aware of it, you will always work on a single active branch while working on your project. When you first create your project in Git, Git creates a branch called master for you by default and you start working on that branch.
Let’s see a simple usage of git branch command.
- Creating a new branch
As far as I know, there are two ways to create a new branch on Git. One of these ways is to use the git branch command and the other is to give the -b parameter to the git checkout command. There is very little difference between them.
git branch: just creates a new branch
git checkout -b: if the branch you wrote the name of first does not exist, will be created a new branch, and the branch you are in will be changed.
2. Changing the name of a branch
git branch -m <old name> <new name>: Change the old name with a new name
Why did we use different branches?
I can say that there are two kinds of branches. Let’s see in detail what I mean.
Short Term / Subject Based Branches
You can easily and quickly create low-cost branches when coding new features, bug fixing, or working on experimental features. Branches created for such purposes have two common properties
These branches are created for a single subject or change. For example, we do not code a new feature like “Login with GitHub” on the branch you created for an error reported to you.
Your work on these branches takes a relatively short time. When our work is complete, we merge and delete these branches to the master or a branch described more broadly.
The second type of branches have a higher level of meaning and represent the stable, test, and development stages of your project rather than focused topics such as new features, bug fixes, and experimental work. Such branches will survive as long as you develop on your project. Typically the following rules apply to such branches
In general, you do not directly make changes on such long-term branches. By making your work on short-term branches, you integrate the changes into these branches.
There is a hierarchy between long-running branches. Usually, the master branch is the stable version of your project, and the less stable development branch is the hierarchically one under which you integrate your developments.
The criteria by which long-term branches will be created, how they will be managed, and what their names will usually vary depending on the team and the project. However, in any case, it should be decided by consensus as a team what kind of branching strategy to follow.
How do we work together thanks to branches?
While you are working in your branch, someone from your group can work in a different part of the code with her/his branch. You can connect your code and your colleague’s code in the master branch after both of you complete your tasks. In this way, you can work together easily.