Git and Github:Git commands  I frequently use
Tags: opensource, git, github
Published: 2020-12-06 14:36:39

Git commands are very important to everyone that needs to track a set of files using a distributed version control system(vcs).

In this post I will be listing commands I use while contributing to open source projects, my personal projects or any git related operation.

To understand distributed vcs better check out this post [Git vs Github](https://www.freecodecamp.org/news/git-and-github-overview/) 

You have a project, folder, directory containing any kind of file be it source code, document, pictures and you want to keep track of the changes you make to them and possibly share them with the public then this can be very helpful.

#### To add git to a directory
* Open you terminal
* Go into the directory
* Type git init

Now that you have added git to the directory you are ready to start tracking the files inside.

#### To add files in the directory to git then type:
* git add . (to add all the files)
* git add \<file-name> (to add only a particular file)
* git commit -m "message" (to finally put them in git with a message to help you understand the changes that actually occurred)

For every changes you make you have the above step so that git will continue keeping track of it. Next step is to push it to a remote repository so that you can always have a copy for when you need those files but don't have access to your computer or you want to share it with the public.

#### To save your file on gitHub do the following:
* Log in to your account on [github](https://github.com) or create one if you are a new user.
* Click the + icon on the top right corner close to your avatar to create a [new repository](https://github.com/new)
* Get the url to the repository you just created by clicking on the repository name, go to code which is highlighted in green colour, click on it and then copy the url

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/tluzrswc4t3c64h1djmv.png)
##### In your terminal you type
* git config --global user.name "gitHub username"
* git config --global user.email "github email"
* git remote add origin \<the url you copied>
* git push -u origin master

Your files now have a remote backup and can be accessed from anywhere.

#### If you added a file but you no longer want it to be tracked by git:
* git rm -r --cached \<folder name>(if its a folder)
* git rm --cached \<file name>(if its a file)
* add the removed file or folder to `.gitignore`
* git add .
* git commit -m "message"  
 
#### You have forked a repository not owned by you and want to stay up to date with changes happening in that repository
* git remote add upstream <url>
* git fetch upstream

And you can update the branch you are working on from the upstream master by typing

* git rebase upstream/master
* git push -f origin branch(this is a force push because the remote repository might reject the changes)

#### To check the remote urls  connected to the project you are working on:
* git remote -v (this lists all the repository url both origin and upstreams connected to your project)

#### Working with branches
* git checkout -b \<branch name>(to create a new branch)
* git checkout \<branch name>(to go into an already existing branch)
* git branch (to list all the local branches)
* git branch -d \<branch name> (to delete a branch)
* git branch -m \<new name> (to rename a branch while in it)

I will continue updating this post as I work with more git commands or remember any which I must have forgotten I used. You can add commands you have used in the comment section and hope this helps someone.