This post presents the basics of using GIT Bash and GitHub for project management in any scale.
Why Git and GitHub
So Git is a Distributed Version Control System for tracking and managing your source code in a project. Distributed in the sense each developer working on the project got a copy of the whole code base, Version control is that software can be got back to previous version as each save point has got a unique address.
There are also *Centralized Version Control Systems such as CVS or Subversion where only a single system got all the code base as a Client-server model.
Git is superior cos of the distributed nature as code is avail everywhere so flexible, remote work is more scalable with out internet access. For this operations Git uses a command line interface(CLI or text based interface) to interact with git, similar to command prompt in windows. For UNIX based OS (Mac OS or Linux) Zsh is the commang line where as for Windows its Power vlcShell or Bash command line.
UNIX systems have these CLI pre-installed, windows user's gotta install git from Git website, its a normal installation process.
So next up we gotta store the repository (whole project files are named 'repository') remotely GitHub provides the same, where all the code is hosted. Which provides a GUI(Graphic user interface) and communicates with the local git repository.
Sign up for GitHub and create a repository
Getting started
We gonna use the Git CLI to create, add, save data in local repository as well loading to GitHub and vice versa.
Start a Work space
Its where your code resides, there are two situations either;
1. Setup a local repository
Now lets create a local project folder any push that to github.
- create a file in your fav code editor.
- Open terminal
Ctrl+` in VS Code editor
- To make this a git repo;
that's it now your folder is tracked by git.git init
- Now add some code to your file and save it.
- Use ;
git status //to see the changes made
- Now let git see the changes to be saved, and then save it to git;
git add "filename" or . git commit -m "commit message"
- Now that all ready push to github, but now git don't know were to push add the origin using;
git remote add origin "your repo url" // link to your repo git remote -v // would check for your repo
- That's it now push the changes;
git push -u origin master // -u is a upstream to remind git this is permanent repo to repeatedly push, use only git push next time.
2. Clone a remote repository from github
As we have created a repository in github we gotta replicate that in our local machine.
- Open your folder on fav code editor.
- Toggle the project folder.
- Open terminal
Ctrl+` in VS Code Editor
- To clone repo use git clone followed by repo url.
You have clone the repo to local machine.git clone https://github.com/shamaayilahmed/demo.git
- Toggle to git folder from git hub.
- check for the hidden .git folder using;
ls -la
- Change the data in code editor and check for changes using;
would clearly mention all the changes.git status
- To save changes to github;
git add "filename" or . //to track the changes git commit -m "commit message" //to save
- Before pushing to github gotta create SSH key using;
manually open file and copy content from id_rsa.pub and paste it on your github profiles/settings/ssh and gpg keys/add new key.ssh-keygen -t rsa -b 4096 -C "your@email" //give a file name or default file is ~/.ssh/id_rsa and a passphrase
- We have to authorize ssh key using;
Thats it you can view thin on SSH keys in github profile.eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_rsa
- Now push change to github using;
git push origin master
Whoa, you have managed a git, made a changes to it and pushed it to github.
Branching
One of the most anticipated feature of git, for developers to work on same project remotely with whole code base. The default branch is master, further branches can be customized.
- Git bash on any of your local repo;
git branch // returns current branch (master)
- Create a new branch with custom name.
git checkout -b *nameofbranch
- Now the new branch got all code from master, but further commits are only changed in current branch, Later we pull request and merge the branch and delete the current branch.
git commit -am "commit message" git switch master // now we are in master and not got any changes git diff *branchname // would display the added code in branch git switch *branchname git push -u origin *branchname
- Common practice is that the developer working on current branch give a pull request in GitHub and if its accepted the senior developer would merge it to master.
git pull // update local repo of master
- Later the branch is deleted.
git branch -d *branchname
Just added a feature to the codebase, but that's not the case what if two branches commits same file this leads to merge conflicts and is to be solved manually
Merge conflicts
- Create a branch and make a change in code base and commit the changes.
- switch to master and make some changes and try to switch to branch would shoot a error ie; merge conflict.
- The conflict has to be resolved manually. But with modern code editors its a easy go.
Resetting changes and commits
What if we staged a change or commited a wrong code, we gonna go back time. Resets are those who go back certain commits or changes.
git reset // unstage the immediate change
git reset HEAD~*anynumber //moves back the number of commit
git reset *commitaddress //moves back to specified commit
git log //state all the commits with address
for any help
git help tutorial //Would provide a complete documentation
That's a lot to grind, But worth knowing.
Hope you all liked the embeds ;)
Comment below your thoughts and shoot me a mail.