You must have heard the term git and github, or maybe you've been using it for a while, but it still seems confusing?
Just for the record, Git and Github are two different things. Git is primarily a version control system that can be integrated literally in any software development pipeline. In contrast, Github is an online platform that provides hosting for software that uses Git as version control.
Git can be tricky. There are times when you just try to push your code, but your branches are out of sync, and you end up wasting much time there.
Let's see how to use Git properly to focus your time on writing productive code.
Install & Setup
Install Git using the following command :
1sudo apt install git
If you are on windows, you show install git bash. Click Here to install git bash in windows.
Setup for newly installed Git
1git config --global user.name "User Name"2git config --global user.email "email"
Basic version control
Go to your project directory and run
git init to make this project as a git repository. This will create a
.git folder in your project, which will be used to store all the versions.
To start with version controlling for an existing project, you can add those files and do an initial commit.
1git add <file>2git commit -m 'first commit'
This version is still a local repository; you can go to Github and create a new repository online and link it to your local git repository.
1git remote add origin \2https://github.com/user/repo.git34### An example with a repository of mine5git remote add origin \6https://github.com/viral-sangani/MeTube.git
Then you can push the code to online Github repository by just running this command,
1git push origin master
voila! You’ve code is on Github!
Knowing the Git
Checking the status
To determine which file are in which state, we use
git status. It allows you to see which files in your project are changed, which files are staged, and which files are committed.
If you create a new file which is not in your project and run this command, see this
1$ git status2# On branch master3# Untracked files:4# (use "git add <file>..." to include in what will be committed)5#6# README7nothing added to commit but untracked files present (use "git add" to track)
This command is probably the most used in Git.
Adding file in git
There are few ways to add files in Git. There are cases when you want to add files manually, but most of the time, we tend to add all the changed files. There are more efficient ways to do so instead of adding all the files manually.
1# Adding files one by one2git add filename34# Adding all files in the current directory5git add -A67# Adding all files changes in the current directory8git add .910# Choosing what changes to add (this will get through all your changes and you can 'Y' or 'N' the changes)11git add -p
Committing your work
Usually, people commit using basic command
git commit -m "<message>, but there are some excellent ways to commit your changes.
1# This is typically used for shorter commit messages2git commit -m 'commit message'34# Add file and commit in one shot5git commit filename -m 'commit message'67# Add file and commit the staged file8git commit -am 'insert commit message'910# Changing your most recent commit message11git commit --amend 'new commit message'121314# Combine a sequence of commits into a single one15# You might use this to organize a messy commit history16git rebase -i1718# This will give you an interface on your code editor:19# Commands:20# p, pick = use commit21# r, reword = use commit, but edit the commit message22# e, edit = use commit but stop for amending23# s, squash = use to commit, but meld into the previous commit24# f, fixup = like "squash", but discard this commit's log message25# x, exec = run command (the rest of the line) using shell
Branch and Merge
The master branch always contains working and stable code. There are some cases where you want to work on new features, or you want to solve some bugs or just want to keep the backup of the current stable version.
Branching helps you to work on a separate copy of your code without harming the master branch. When you first create a branch, it will create an exact copy of the current version; then, you can make changes independently in the new branch. Once you are confident enough and your new feature is ready, you can just merge it in the master branch.
Just remember, it's not a good practice to do the changes directly on the master branch. Always create a branch, make the changes, and merge the code into master.
1# Create a local branch to work on2git checkout -b branch_name34# Switching between 2 branches5git checkout branch_16git checkout branch_278### Pushing your new local branch to remote as a backup9git push -u origin branch_21011# Deleting a local branch - this won't let you delete a branch ### that hasn't been merged yet12git branch -d branch_21314# Deleting a local branch - this WILL delete a branch even if it ### hasn't been merged yet!15git branch -D branch_21617# Viewing all current branches for the repository, including both ### local and remote branches. Great to see if you already have a ### branch for a particular feature addition, especially on more significant ### projects18git branch -a192021# Viewing all local branches22git branch2324# Viewing all remote branches25git branch -r2627# Rebase master branch into the local branch28$ Git rebase origin/master2930# Pushing local branch after rebasing master into the local branch31$ git push origin +branchname
Now, let's see how to merge the branch to the master branch.
1# First make sure you're looking at the master branch2git checkout master34# Now merge your branch to master5git merge branch_2
There are some cases when you can get a CONFLICT error while merging the code. Git will show you the exact file where CONFLICT occurred, and after resolving the CONFLICT, you have to commit the current version.
Things go wrong sometimes, and we should know how to fix it. Git has everything you need to make a mistake with the code you push, overwrite something, or just want to correct something you pushed.
1### Switch to the version of the code of the most recent commit2git reset HEAD3git reset HEAD -- filename # for a specific file45### Switch to the version of the code before the most recent commit6git reset HEAD^ -- filename7git reset HEAD^ -- filename # for a specific file89### Switchback 3 or 5 commits10git reset HEAD~3 -- filename11git reset HEAD~3 -- filename # for a specific file12git reset HEAD~5 -- filename13git reset HEAD~5 -- filename # for a specific file1415### Switch back to a specific commit16### Where the '23FE2RE' is the commit ID17git reset 23FE2RE -- filename18git reset 23FE2RE -- filename # for a specific file1920### The previous commands were what's known as "soft" resets.21### Your code is reset, but Git will still keep a copy of the other code22### handy in case you need it. On the other hand, the --hard flag23### tells Git to overwrite all changes in the working directory.2425git reset --hard 23FE2RE26### Think before using this command
Tips for Git
If you are working in the team, and because of some noob developer, your code gets corrupted or you end up having issues. For cases like this, git provide a tool called
blame, which will tell you precisely whom to blame for the issue you are facing.
1### Show alteration history of a file with the name of the author2git blame 'filename'34### Show alteration history of a file with the name of the author5### and the git commit ID6git blame 'filename' -l
git log most of the time.
It will show you all the commits in your branch and will show you the current status of other branches too.
1### Show a list of all commits in a repository. This command shows2### everything about a commit, such as a commit ID, author, date and3### commit message.4git log56### List of commits showing only commit messages and changes7git log -p89### List of commits with the particular string you are looking for10git log -S 'something'1112### List of commits by author13git log --author 'Author Name'1415### Show a summary of the list of commits in a repository. This16### shows a shorter version of the commit ID and the commit message.17git log --oneline1819### Show a list of commits in a repository since yesterday20git log --since=yesterday2122### Shows log by author and searching for a specific term inside the23### commit message24git log --grep "term" --author "name"
A newsletter that sparks joy.
This year I'm focusing big-time on writing content for web developers. Expect rich and bite-sized articles. There will be no spam and you can unsubscribe at any time.