Git Commands for beginners!
Tags: git, github, bash, beginners
Published: 2020-09-22 22:22:46

##**Configure Git**

<div class="highlight"><pre class="highlight shell"><code><span class="c"># list all the configured values</span>
git config <span class="o">--list</span> 

<span class="c"># Set username & email for git globally</span>
git config <span class="o">--global</span> user.name <span class="s1">"Progressive Programmer"</span>
git config <span class="o">--global</span> user.email <span class="s1">"progressive@gmail.com"</span>

<span class="c"># Set committer name & email for git</span>
git config <span class="o">--global</span> committer.name <span class="s1">"Programmer"</span>
git config <span class="o">--global</span> committer.email <span class="s1">"programmer@gmail.com"</span>
</code></pre></div>






##**Create a new Repository**

<div class="highlight"><pre class="highlight shell"><code><span class="c"># Create a new folder and initialize git repo</span>
git init name <span class="o"></span> 

<span class="c"># Clone a remote repo in your local system</span>
git clone url_of_repo <span class="o"></span>
</code></pre></div>





##**Add files/folders to Staging Area**

<div class="highlight"><pre class="highlight shell"><code><span class="c"># Adds mentioned file/folder to the staging area</span>
git add hello.py<span class="o"></span> 

<span class="c"># Adds all the files and folders in the current directory </span>
git add . <span class="o"></span>

<span class="c"># Opens the file and let you choose <br/>the portion to be added for next commit  </span>
git add <span class="o">-p </span>hello.py
</code></pre></div>


##**Commit Changes**

<div class="highlight"><pre class="highlight shell"><code><span class="c"># Commit a snapshot of staged changes</span>
git commit <span class="o"></span> 

<span class="c"># Commit a snapshot of changes in the working directory. </span>
git commit <span class="o">-a</span>

<span class="c"># Shortcut for commit with a commit message </span>
git commit <span class="o">-m</span> "commit message"
</code></pre></div>







##**Git Branching**

<div class="highlight"><pre class="highlight shell"><code><span class="c"># Create a new branch</span>
git branch <span class="o"></span>crazy_experiment

<span class="c"># List all branches </span>
git branch
<span class="c"># List both remote and local branches </span>  
git branch <span class="nt">-a</span>
<span class="c"># List only branches that match the pattern mentioned </span>
git branch <span class="nt">--list </span> 'pattern here'
</code></pre></div>


##**Switch | Delete | Rename Branch**

<div class="highlight"><pre class="highlight shell"><code>
<span class="c"># Move to different branches</span>
git checkout <span class="o"></span>branch_name
git switch <span class="o"></span> branch_name

<span class="c"># Shortcut to create a new branch and switch to the branch  </span>
git switch <span class="nt">-c</span> <span class="o"> </span>new_branch_name
git checkout <span class="nt">-b</span> <span class="o"> </span>new_branch_name 

<span class="c"># Delete the given branch </span>
git branch <span class="nt">-d</span> <span class="o"> </span>trash
<span class="c"># Force delete the given branch </span>
git branch <span class="nt">-D</span> <span class="o"> </span>trash

<span class="c"># Rename the current branch </span>
git branch <span class="nt">-m</span> <span class="o"> </span>new_name
</code></pre></div>


##**Merge Branch**
<div class="highlight"><pre class="highlight shell"><code>
<span class="c"># Merge given branch name with the working branch</span>
git merge <span class="o"> </span>branch_name
<span class="c"># Continue merger after conflict resolution</span>
git merge <span class="o"> --continue </span>
</code></pre></div>







##**Check Status**

<div class="highlight"><pre class="highlight shell"><code><span class="c"># Shows the working tree status</span>
git status <span class="o"></span> 

<span class="c"># Shows status of working tree in a short format </span>
git status <span class="o">--short</span>

<span class="c"># Shows status of branch in a short format </span>
git status <span class="o">--branch</span>
</code></pre></div>

_________________________

##**Remote Repository**
<div class="highlight"><pre class="highlight shell"><code>
<span class="c"># Lists remote repo name and url(fetch/push) </span>
git remote <span class="o">-v</span>

<span class="c"># Add a remote repository with local repository </span>
git remote add <span class="o">origin</span> url_remote_repo

<span class="c"># Remove the remote repo with given name</span>
git remote remove<span class="o"></span> repo_name

<span class="c"># Rename the remote repo with given name</span>
git remote rename<span class="o"></span> old_name new_name

<span class="c"># Updates remote repo with local repo </span>
git push 
</code></pre></div>

###I hope this helps !!🚀🚀

### **Which is your favorite git command??**

### Don't forget to drop your thoughts and feedback in the comment section. Also, feel free to suggest any addition to this list..