Git Basic commands

what is Git?

Git is a version control system used for tracking changes, collaboration, and to push files to remote repository.

Then what the heck is github?

Github (Microsoft owned) is a service provided for git users to store our files(code) online which is called remote repo.

some of the popular alternatives to github are bitbucket and bitbucket.

Git workflow

simple workflow of a git

Initialize git in workflow directory -> add files to staging -> commit changes -> push to repo

you can do these things by learning the commands below :

Commands

SR.NOCOMMAND NAMEDESCRIPTION
1git config –global user.email <email>
git config –global user.name <name>
for first time you need to configure your name and email, to tell the git who is committing the changes to repo.
2git init“git init” is used to initialize local repo in our working directory. This is the first command to be used before any git command.
3git status“git status” will display the status of our files that include the untracked files , changes to be committed and the branch we currently working on.
4.git add“git add .” will add all the files in our directory to staging. we can also use “git add <file_name> to add a specific file to staging
5.git rm -cached <file_name>it is reverse command of “git add” . used to remove a file from staging.
6git commit -m <message>after staging it is used to commit with a message included of what we modified.
7git logdisplays all the commits you have made till now ( contains author of the commit , date of commit made)
8git checkout <id>“git checkout <id>” is a time-machine command used to go back to the previous commit
9git revert <id>git revert is used go back in time to one commit.
10git reset [id]“git reset –hard [commit_id]” is used to go back in time multiple times and stays there and discard the changes made after that.
Note: messing with commit history of a repo is dangerous
11.git branchit displays the branches present in your repo.
12git branch [branch_name]it create a new branch
13git checkout [branch_name]shifts from your current branch to the specified branch
14git branch -d [branch_name]will delete a specified branch
15git merge [other_branch]will merge the other_branch to the current branch you are in.
16git clone [http_link]git clone is how you get a local copy of an existing repository to work on
17git remote add origin [link]this command able to help us to connect the local repo to the github repository
18git pull origin maingit pull(fetch+merge) is how you update that local copy with new commits from the remote repository
19git push origin mainpush the changes in your local repo (main brach) to the remote repo
20git push origin –delete [branch_name]used to delete a branch from a remote repo