Git and GitHub
git is a distributed version control system
version control helps to track and manage code changes over time.
distributed means that the developer’s computer has a complete copy of the code base.
to check whether you have installed git properly the command is
git - versionnext you need to configure git to work with your name and email
git config - global user.name 'faith-25'
git config - global user.email 'faithmkyalo9@gmail.com'note- after every command, you press enter
Repositories
this is where git tracks everything in your project
to create a repository in git,
git initto change the branch name from ‘master’ to ‘main’ we use;
git config - global init.default branch mainafter changing the settings, we create a new folder and open it within your editor and then re-run git init
git initthe branch name changes to main
branch is a parallel version of your project
to add a file(s) to a repo
git add .then after adding the file, we commit it
git commit -m "first commit"if you would like to see the git command history,
git logto restore back to a git command that you had done before in situations where the commit introduces a buggy code, we can use a new command, you copy the commit hash of the commit you created first, the hash is found on the git command history, then exit the git log by pressing the q letter on the keyboard then,
git checkout (paste the hash you copied)to go back to a current state which is called the head state(refers to the pointer pointing to the latest commit that was created.
git checkout main or
git checkout -f mainGitHub is a cloud platform that allows you to store you git repositories online and collaborate with others
to push your local project to GitHub you’ll need to link it to a remote repository
there are 2 types of repositories
local repository — this is a version of a project that exists on your own machine
remote repository — this is a version of a project stored on a server like GitHub, GitLab or bit bucket
when you clone a repository from GitHub, git automatically renames the remote repository as origin by default.
to switch from master branch to main
git branch -M mainlinking our local repo to a remote origin
git remote origin (then paste the link that you copied from the repository)to push your local commits to GitHub use
git push -u origin mainBRANCHING
Get Faith Rosella Kyalo’s stories in your inbox
Join Medium for free to get updates from this writer.
branching in git allows you to create different versions of your project like a copy of your project
to create a new branch, run
git branch (branch-name)if you want to switch to the newly created branch from the main branch, you run
git checkout (branch-name)if you want to get back to main branch run,
git checkout mainto create a branch and to switch to it immediately,
git checkout -b (branch-name)note: when you create a new branch, it will be based on the branch you’re currently on
when you make the changes in a file of the new branch, you git add ., then make a commit then push it using
git push -u origin (the name of the branch)[when you create a new branch in local repository but doesn’t appear in the remote repository, you need to sync them, to see those changes, you have to first publish your local branch,
git push - set-upstream origin (the name of the branch) OR
git push -u origin (the name of the branch)]an upstream branch is a remote branch that your local branch tracks
if you want to push something from your local branch to your remote branch, you simply have to run
git pushgit pull command downloads the latest changes that have been made in the remote repository and merges them into your local repo for that branch
git pullif you want to merge changes from one branch to the main branch, for example i have 2 branches the main branch and the feature-branch and there are changes i made in this feature-branch and i want to incorporate them to the main branch, you must open up a pull request.
a pull request(PR) lets you share changes with your team for review and feedback. if approved then the changes can be merged to the main branch.
to open up a pull request, click compare and pull request, select new pull request, select the branch you want to merge from i.e feature-branch and then to the branch you want to merge to, click create pull request, then click create pull request that appears again.
after that process, reload the browser and click merge pull request, then click confirm merge.
SUMMARY OF THE WORKFLOW
- clone a repository
- create a new branch from the main branch or another branch
- make your changes
- push the branch to the remote repository
- open up a pull request
- merge changes
- pull the merged changes into your local main branch
- repeat from step 2
RESOLVING MERGE CONFLICT
A merge conflict is a situation where git is uncertain about which part to merge.
to solve the issue/conflict, shift to the main branch, then you run pull
GIT savior commands
- git checkout <commit-hash> this is a way to view history of the past commits.
- git reset- it allows us to remove the traces of commits in history but gives us the changes we made in the discarded commits
get reset - soft <commit-hash>
get reset - hard <commit-hash>- git revert- it allows you to make changes to an old commit without losing the logs of other commits that come after it. in a situation where you deployed a feature and broke production and you want to undo its effects.
- git stash- it lets you save your uncommitted changes both staged and un staged without committing them. run,
git stashif you are maybe done with what you were doing before and you want to return to your code you run
git stash list\
hit stash apply (the stashlist eg stash@{0})






