if you’re new to git, follow the steps below to get comfortable making changes to the code base, opening up a pull request (PR), and merging code into the master branch.
Git is a version control system for tracking changes in the code. Git is the software or tool on which Github is built. You can use git and never use github and vice versa. But most of the companies uses Github to collaborate on projects, so you should learn both.
Also, distributed version-control system is just a fancy term for “Saving different snapshot or copy of your code”. Simple isn’t it?
Step 0: Install git and create a GitHub account
In order to use Git, You need to first install it in your computer and create an account on Github.com. It’s free to signup and use Github.
Step 1: Creating a folder or local repo
gourav@gourav:~$ cd Desktop/ gourav@gourav:~/Desktop$ mkdir myrepo gourav@gourav:~/Desktop$ cd myrepo/ initialize a git repository by using git init
Step 2: Add a new file to the repo
gourav@gourav:~/Desktop/myrepo$ touch gourav.txt gourav@gourav:~/Desktop/myrepo$ ls gourav.txt **touch** here creates an empty file **ls** command lists all the files in your repo
Step 3: Check status
After creating the new file, you can use the
git status command to see which files git knows exist.
gourav@gourav:~/Desktop/myrepo$ git init Initialized empty Git repository in /home/gourav/Desktop/myrepo/.git/ gourav@gourav:~/Desktop/myrepo$ git status On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) gourav.txt nothing added to commit but untracked files present (use "git add" to track)
Step 3: The staging environment and commit
Staging is a step before the commit process in git.
A commit is a record of what files you have changed since the last time you made a commit. Essentially, you make changes to your repo (for example, adding a file or modifying one) and then tell git to put those files into a commit.
To add a file to a commit, you first need to add it to the staging environment. To do this, you can use the [git add] filename command (see Step 4 below).
Step 4: Add a file to the staging environment
Add a file to the staging environment using the git add command. We can use [git add .] command to add all the files to the staging.
gourav@gourav:~/Desktop/myrepo$ git add . gourav@gourav:~/Desktop/myrepo$ git commit -m "my first commit" [master (root-commit) 431b347] my first commit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 gourav.txt
Step 5: Create a new branch
Say you want to make a new feature but are worried about making changes to the main project while developing the feature. This is where git branches come in.
Example: if you want to add a new page to your website you can create a new branch just for that page without affecting the main part of the project. Once you’re done with the page, you can merge your changes from your branch into the master branch. When you create a new branch, Git keeps track of which commit your branch ‘branched’ off of, so it knows the history behind all the files.
git checkout -b <my branch name>. This command will automatically create a new branch and then ‘check you out’ on it.
gourav@gourav:~/Desktop/myrepo$ git checkout -b my-new-branch Switched to a new branch 'my-new-branch' gourav@gourav:~/Desktop/myrepo$ git branch -a master * my-new-branch
The branch name with the asterisk( * ) next to it indicates which branch you’re pointed to at that given time.
Now, if you switch back to the master branch and make some more commits, your new branch won’t see any of those changes until you merge those changes onto your new branch.
Step 6: Create a new repository on GitHub
If you only want to keep track of your code locally, you don’t need to use GitHub. But if you want to work with a team, you can use GitHub to collaboratively modify the project’s code.
To create a new repo on GitHub, log in and go to the GitHub home page. You should see a green ‘+ New repository’ button:
GitHub will ask if you want to create a new repo from scratch or if you want to add a repo you have created locally. In this case, since we’ve already created a new repo locally, we want to push that onto GitHub so follow the ‘….or push an existing repository from the command line’ section:
Step 7: Add remote origin
gourav@gourav:~/Desktop/myrepo$ git remote add origin https://github.com/gouravthakur39/myrepo.git gourav@gourav:~/Desktop/myrepo$ git push -u origin master Username for 'https://github.com': gouravthakur39 Password for 'https://firstname.lastname@example.org': Enumerating objects: 3, done. Counting objects: 100% (3/3), done. Writing objects: 100% (3/3), 216 bytes | 216.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://github.com/gouravthakur39/myrepo.git * [new branch] master -> master Branch 'master' set up to track remote branch 'master' from 'origin'.
Step 8: Push a branch to GitHub
To push changes onto a new branch on GitHub, you’ll want to run
**[git push](http://git-scm.com/docs/git-push) origin yourbranchname**. GitHub will automatically create the branch for you on the remote repository:
gourav@gourav:~/Desktop/myrepo$ git push origin my-new-branch Username for 'https://github.com': gouravthakur39 Password for 'https://email@example.com': Total 0 (delta 0), reused 0 (delta 0) remote: remote: Create a pull request for 'my-new-branch' on GitHub by visiting: remote: https://github.com/gouravthakur39/myrepo/pull/new/my-new-branch remote: To https://github.com/gouravthakur39/myrepo.git * [new branch] my-new-branch -> my-new-branch
origin is essentially shorthand for the remote repository’s URL. So, to push your changes to the remote repository, you could’ve used either the command:
git push firstname.lastname@example.org:git/git.git yourbranchname or
git push origin yourbranchname
If you refresh the GitHub page, you’ll see note saying a branch with your name has just been pushed into the repository. You can also click the ‘branches’ link to see your branch listed there.
Step 9: Create a Pull Request (PR)
Now, click the green button that say compare and pull request.
Step 10: Merge a PR
Go ahead and click the green ‘Merge pull request’ button. This will merge your changes into the master branch.
When you’re done, I recommend deleting your branch (too many branches can become messy), so hit that grey ‘Delete branch’ button as well.
Step 11: Get changes on GitHub back to your computer
git pull origin master command (when working on the master branch).
You can use the
git log command again to see all new commits.
Learn and explore more
You’ve successfully made a PR and merged your code to the master branch.