I've been working for a team project lately and I faced some problems while working because it was the first time working with other people for me and also I didn't know how to use Git/GitHub for a team project. I am sure there are or will be some people might struggle like I did. So I thought it would be good to share what I've learnt.
So, here are a few things I learnt about.
What is branch? How do I make my own branch to work on?
So, if you haven't worked with other people or if you are not so familiar wit GitHub, you might not have worked on a branch. That was exactly me. I was just pushing my code, all of them to main branch. However, if you are working on a team project, you definitely will be working on your or specific branch depends on what you do. In our project, we made a branch for each person and depending on a function or for improvement, we created new branches and worked there.
The reason that we do this is because if we work on the same branch, the code that I write will effect on other people's code but if we work on different branches, they don't effect on other branches and lots of people can work for one project together at the same time and later we can merge those changes on to the main branch.
Important Always check which branch you are on before commit or push to avoid pushing your code to the main directly or to wrong branch.
git commands
// Create branch
$ git branch <branchname>
// E.g - Create a branch named feature#10
$ git branch feature#10
// Check current branch
$ git branch
// Change branch
$ git checkout <branchname>
// e.g) Go to a branch named feature#10
$ git checkout feature#10
(Switched to branch 'feature#10')
// Create and checkout to that branch at once
$ git checkout -b <branchname>
// e.g) Create a branch named issue5 and go to a branch
$ git checkout -b issue5
// Delete branch
$ git branch -d <branchname>
What does it mean when somebody tells me to send a pull request?
If you send a pull request, GitHub lets other people know about changes you've pushed to a branch so you and your teammates can review and discuss about those changes.
Creating a pull request is very easy.
When you push a new code to a branch, a message appears like this.
Click the Compare & pull request
button!
Then you can choose from which branch you’d like to merge to a main branch.
Check if it’s selected right branch(the branch you worked on)
Also you can leave a message about changes you've made or a comment that you want your teammates to read.
When it’s done, click the Create pull request
button down below on the right-hand corner.
If you see this page, this means you’ve created your pull request! and your teammates will get the notification and will see the comment you left with your pull request.
Also, if you have an authority to merge, you will be able to merge by clicking Merge pull request
as well.
How can I apply those changes if things are added or something changes on main branch while I am working on other branch?
When I tried to commit and push my code to the branch I was working, it didn't work. The reason it didn't work was because someone merged their code to the main branch and something has changed from the main code that I had on local branch(what I currently have on my computer). So I tried to pull the main code but it said "Already up to date"
However, there were definitely changes. I found this solution by searching and this was the blog post that helped me to understand.
rebase vs merge
As it's name itself, rebase means re-base. It changes the base of the branch. So when you write the command git rebase main
, it moves the base of the main that has older code to the most recent main which is recently updated main on GitHub.
git commands
$ git rebase main
Article ot read: Merging vs. Rebasing by Atlassian
I tried to send pull request / merge but github says there are conflicts and I don't know how to fix this.
Then why conflicts happens?
Conflicts generally arise when two people have changed the same lines in a file, or if one developer deleted a file while another developer was modifying it. In these cases, Git cannot automatically determine what is correct. ... Git will mark the file as being conflicted and halt the merging process.
Source
So, this happened me as well. I tried to send the pull request and got a message like this
When you click Resolve conflicts
button,
You will be sent to a page like above and you can see codes that are repeated and things with conflicts.
You can manually change/edit the code there and when you are done, click Mark as resolved
button on the right-hand side on the top.
This changes will be applied and you will see the checked icon in green like below image.
When you solve those conflicts then you can send pull request like you did before!
Applying changes when I delete file
When you type git command git add . this will add all the changes to the stage(staging is what you are doing before commit. You cannot commit without staging. So, you have to add the file(do the staging) that you changed or created whatever that you'd like to commit and push to the branch. Then you can go to the next step which is git commit). However, I didn't wanted to add all the changes and I was adding those changes one by one by using command git add <filename>
since I wanted to stage a few selected files only.
Adding a file
// e.g) if you'd like to add a file named "modal.js" that is in the components directory,
$ git add /components/modals.js
But this doesn't include changes about deleted file and I wanted to add that as well. This command will add that as well.
Include deletion to the stage (deletion will be staged which deletes the file from the branch)
$ git add -u
Also I sometimes mistyped and wanted to cancel git add or commit that I made.
undo git add or git commit that was made.
// cancel all staging. this will delete everything from staging area.
$ git add *
// delete specific file from staging area
$ git reset HEAD <filename>
// undo git commit
$ git reset HEAD^
// change commit message before it's pushed (when it's still in local)
$ git commit -amend
// after it's edited, click esc and type :wq (this will save and close the tab)
// you can check the current status - which files or directories are staged or committed.
$ git status
How to sync(update) forked repository with the original repository to keep it up to date?
My team project was at my teammate's GitHub so I wanted to have it on my GitHub as well. So I forked the repository it didn't change the code from the time that I forked. I wanted to sync and update it to the latest version we had.
First, clone the forked project and go to the folder(directory) where the project is.
$ git clone <forked repo url>
$ cd <directory(folder) where the cloned project is>
// for example if it's in the project folder
// cd project
Next, add git remote to the original repo
$ git remote add upstream <original repo url>
If you added the remote, check if it's applied correctly.
$ git remote -v
Now, you have to get the latest updates from upstream repo which is the original repo
$ git fetch upstream
Then merge this latest version to my forked repo
$ git merge upstream/main
Lastly, push the updates to my GitHub, then my forked repo will be updated to the lated version.
$ git push
Top comments (2)
Amazing post! I needed this! Thank you so much for providing so much value to the community 🙌
Very well written. Nice post.