Skip to content

Latest commit

 

History

History
152 lines (117 loc) · 4.95 KB

1.git_cmds.md

File metadata and controls

152 lines (117 loc) · 4.95 KB

Git Commands - An Overview

I have tried to summarize some of the basic Git commands I demonstrated recently. Before starting that, let me make a few points:

  • The standard name for the default branch is main. It used to be master until we realized it had roots from racist terminology and decided to change it.
  • The standard names for the default remote are origin and upstream. For forked repositories, origin is commonly used for the fork and upstream for the parent.
  • Not all the Git commands are listed below. You shall continue to explore and discover how to utilize Git on your own as you use it.

The exercise is available in this repository, and a recording of the repeated session is available here on YouTube.

git config

To manage i. e. view and update Git variables. I am including this at the top because I missed it during yesterday's session.

Every commit carries a signature of the person who made it. It is a bad practice not to include information regarding the commit's author. Ensuring that, we need to configure Git to identify us. This configuration is one-time and will be saved on the device. I urge you to do this right now.

My name is Param Siddharth and my e-mail address is [email protected]. In this example, I will do the one-time global configuration by executing the following commands:

git config --global user.name 'Param Siddharth'
git config --global user.email '[email protected]'

Replace the name and e-mail address with your own. This will save your details for all future commits, and is essential for using Git. Make sure you use your public e-mail address and not a private one.

To see the current configuration, execute:

git config --list

git init

Initializes a repository in the current directory.

git status

Displays the current status of the repository.

git add

Used to add files to the staging area for the next commit. E. g. To add yay.txt, execute

git add yay.txt

We usually need to add everything in the current directory to the staging area. For that, we execute:

git add .

git commit

Saves the current staged changes as a new commit.

Remember to always add a short comprehensible commit message. Don't forget to add the changes to the staging area with git add . before committing.

git commit -m 'Your message here'

git branch

Retrieve the current/all branch[es] or make a new one.

The following will tell you the name of the current branch.

git branch

To create a new branch named prasad, use the following:

git branch -M prasad

git log

Retrieve a list of all commits in the currently active branch.

Press q to exit and arrow keys to navigate.

git log

For a simpler representation of the list, use the --oneline switch.

git log --oneline

git remote

Retrieve, add, or update remote addresses.

Use the following to retrieve a list of remote repositories with their respective URLs:

git remote -v

Let's say you want to add a new remote with address https://www.kya.bat/hai.git and name it iamahero. To do that, execute:

git remote add iamahero https://www.kya.bat/hai.git

git push

Push changes to a remote repository.

Let's say you want to push the updates from branch prasad to remote iamahero. For that, execute:

git push -u iamahero prasad

git pull

Pull changes from a remote repository, id est antonymous to git push.

Let's say you want to pull the updates from branch prasad of remote iamahero. For that, execute:

git pull iamahero prasad

git clone

Clone an online repository locally.

To clone a remote repository identified by https://www.kya.bat/hai.git, execute:

git clone https://www.kya.bat/hai.git

This will do the following:

  • Create a local directory with the same name as the repository (here, hai).
  • Clone the remote repository into the folder.
  • Add a remote named origin (semantically so) with address https://www.kya.bat/hai.git to the clone.

Note that the clone will include the entire commit history of the remote repository. If you don't need old commits and want to save space, use the following instead:

git clone --depth 1 https://www.kya.bat/hai.git

git reset

Resets to an older commit.

Let's say you want to revert to previous commit identified as 07e239f. For that, use:

git reset 07e239f

Doing a hard reset means get rid of the current staging area and uncommitted changes as well as revert to the desired commit. To do a hard reset, use:

git reset --hard 07e239f

The last commit is always identified as HEAD. Hence, commonly, to reset the workspace to the last commit, we execute the following:

git reset --hard HEAD

Made with ❤ by Param.