- Necessary git commands
- Getting started with github
- Making your first commit
- Making your first open source contribution
- Merge and Rebase
- FAQs
- What if I made changes that I don't want to commit?
- What if I accidentally made a commit?
- What if I already have code written?
- How do I revert changes back to a specific commit?
- What's the difference between
git pull
andgit fetch
? - How do I create a new branch?
- What's the difference between
git merge
andgit rebase
? - I am getting an error that isn't listed here
- What we learnt
Command | Description | Example |
---|---|---|
init | initializes a new git repository in current directory | $git init |
clone [repo url] | create a local copy (clone) of an existing repository | $git clone https://github.com/<username>/<repository-name>.git |
add [file(s) | <.>] | adds file changes to the staging area so that they can be committed | $git add file1 file2 folder |
commit -m <message> | records changes to the repository, creating a new commit with a descriptive message | $git commit -m "Placeholder message" |
status | displays the current state of the repository, including changes that have been staged or not staged for commit, untracked files and more | $git status |
log | displays a chronological list of commits in the repository, showing commit hashes, authors, dates, and commit messages | $git log |
branch | creates a new branch with the specified name. branches allow for parallel development and isolation of features or fixes | $git branch <branch-name> |
checkout <branch-name> | switches to a different branch or a specific commit, updating the working directory to match the state of the chosen branch or commit | $git checkout <branch-name> |
pull | fetches changes from the remote repository and merges them into the current branch | $git pull |
push | uploads local changes to the remote repository, making them accessible to others | $git push |
What is "github"?
GitHub is a web-based platform built around Git, a distributed version control system. It provides hosting for software development projects using Git repositories and offers a wide range of features aimed towards collaboration, code review, and project management.
Go to github and sign up to create a new account if you don't already have one.
What is "git?"
Git is a version control system made to track changes during development of a software based project. It is used by developers to manage collaboration, different versions of files and much more
Linux
Use your respective package managers to install git, for example on debian based systems
$sudo apt install git-all
Windows
- Download the installer -> exe link
- Follow the installation wizard (gui)
- Set your username and email
- username:
git config --global user.username <username>
- email:
git config --global user.email <email>
- username:
NOTE: Do not change any preset installation setting in the wizard (unless you know what you are doing)
There are two ways of setting up remote access privilege on github HTTP and SSH, but in this session we will be focusing on only one of them, ie, HTTP
HTTPS
- Create a public access key
- If you are on linux, heres how you use credentials for github
git config --global credential.helper 'cache --timeout=31536000'
- If you are on windows, use
git config --global credential.helper manager
and then you should get pop ups for credentials - Go to settings page (click on user icon on top right then settings)
- Scroll down till you see developer settings and click on it
- Go to personal access tokens, then tokens classic
- Generate a new token, then click classic
- Give it necessary permissions
- Copy access token
- Pull a repo using http, then paste access token when prompted for a password
NOTE: The access token is only visible once and when the tab is closed its not viewable again so don't forget to save it
For this example we will be creating your own personalized README.md
file which gets displayed whenever someone views your page.
What is a "repository"?
So has everyone use google drive to store things before? Good, a repository is like google drive but for code. It is a cloud storage space where typically software projects are stored and managed for hosting and collaboration purposes. Basically it is one big folder filled with code files necessary to make a project work out of the box, so that it becomes easy to share projects with others.
Steps to create one
- Go to your repositories tab ->
https://github.com/<username>?tab=repositories
and click onNew
- Name your repository your username, ie for example my repository is called
Aditya-Jyoti
which is my<username>
- Give it a description (optional)
- Make it public
- Click on
Add a README file
- Select a license (pick MIT)
- Click on
Create repository
- Clone the repository
$git clone https://<username>//<username>.git
- Open the repository and the README.md in your favorite text editor
- Make changes, example add a description of yourself
- Add the file to staging
$git add README.md
- Add a descriptive commit message
$git commit -m "Update readme"
- Push the repository to github
$git push
git remote add origin <url>
- Add, Commit and Push your changes
Now you can use your repo normally like you would anytime else.
Head on over to your first open source project called Introduce Yourself and read its CONTRIBUTING.md file. It should have all instructions provided and should get you started with contributing to that project.
Pray to god that you don't have to come to this, but now that you have here's what merge hell is
Think of merging like stacking your new Lego pieces on top of your existing structure. You keep both the original bricks and the new ones, creating a taller tower. This is like when you have your main project (let's say a castle), and you want to add a tower (new feature) to it. You just put the tower on top, and now your castle has a new addition.
How to git merge?
- First move to the branch you want to merge changes into
git checkout <branch-name>
- Now it is a good practice to fetch your changes
git fetch
The main difference between git pull
and git fetch
is that git pull auto merges for you,
hence why it may and may not work during merging.
- Now merge the branch onto your current branch
git merge <merging branch>
- Resolve conflicts
- Go to your code and you'll see some arrow
>>>
or<<<
and equals===
>>>
means the start of the change<<<
means the end of the change- Everything on top of the
===
is the content from current branch and everything below is the content from branch being merged. - delete arrows and equals as you go keeping or removing stuff as you go
- Add, Commit and Push your changes
It's like taking your tower apart and rebuilding it with the new pieces added in between. So, instead of stacking the new pieces on top, you sort them out and insert them into your original tower one by one. This way, everything stays in order. This is similar to realizing you built your tower (branch) in the wrong spot, so you take it apart and rebuild it in the right place with the new pieces added in where they fit better.
How to git rebase
- Set rebase to true
git config pull.rebase true
- Git pull
- Now you should see a bunch of warnings and it should pop you into rebase mode
- Use
git rebase --continue
to move on to the next commit, if its the end of the rebasing commits then it moves to main - Use
git rebase --abort
to get back to the state before you rangit rebase
- Just like merge go on and keep/remove changes that you want/don't want and keep adding and committing then
- Once done
git push
Here we use the git stash
command to temporarily store changes in your working directory without committing them
- stash you changes
git stash
- apply your changes
git stash apply
extra
git stash list
: check your stashed changesgit stash apply stash@{number}
: apply a specific stashgit stash pop
: apply stashed changes and remove it from the listgit stash clear
: remove all stashed changes
So you made some changes that you accidentally committed? Here we can revert to last commit and stash new changes to get a new working space
- run
git reset HEAD^
to undo the last commit - run
git stash
to stash your changes
Now you should be back to where you were without having committed.
NOTE: only stash if you don't want to use the changes that you just made
There are times when you have already written down some code so cloning a repo then adding your changes becomes a pain. Thats where git init
command shines.
Here's how you initialize a git repo locally and then add it globally to github.
- Go to any folder which you want to make a git repo of
- Type in
git init .
this should make the current folder a git repo (adds a .git file) - Now go to github and create a black repo
- Copy its clone link (http/ssh)
- Come over to where you initialized a new repo and add a new remote
If you want to go back to a specific point in your project's history, you can use the git checkout
command followed by the commit hash you want to revert to.
- Find the hash of the commit you want to revert to by using
git log
. - Run
git checkout <commit-hash>
to revert your project's state to that specific commit.
git pull
and git fetch
are both used to update your local repository with changes from a remote repository, but they work slightly differently.
git pull
: This command fetches changes from the remote repository and merges them into your current branch. It's essentially a combination ofgit fetch
andgit merge
.git fetch
: This command only fetches changes from the remote repository and stores them in your local repository. It doesn't automatically merge them into your current branch. You'll need to usegit merge
orgit rebase
to incorporate the fetched changes into your branch.
Creating a new branch allows you to work on new features or bug fixes without affecting the main codebase. Here's how you can create a new branch:
- Run
git checkout -b <branch-name>
to create a new branch and switch to it in one step. - Make your changes on the new branch.
- When you're ready to merge your changes into the main branch, you can use
git merge
or create a pull request on GitHub.
Both git merge
and git rebase
are used to integrate changes from one branch into another, but they do it in different ways.
git merge
: This command integrates changes by merging the commits from one branch into another. It creates a new commit to record the merge, preserving the commit history of both branches.git rebase
: This command rewrites the commit history by moving the changes from one branch onto another branch. It creates new commits for each commit in the original branch, resulting in a linear history.
Choosing between git merge
and git rebase
depends on your workflow and preferences.
Google is your best friend. Google what error you are getting and there will almost always be someone who has faced the issue you are facing before hand.
If google doesn't help then turn to your favorite LLM for help like ChatGPT or Phind.
You could even open up an issue
in this repository and I will reply back when I get time.
So we learnt
- What is git
- What is github
Then we learnt how to
- Create a github repository
- Clone, Add, Commit and Push changes to that repository
- Contribute to open source projects
And then we stepped into the tough parts and saw what is
- Git merge
- Git rebase