Git

Git

What is Git?

Git is a distributed version-control system for tracking changes in source code during software development. It is designed for coordinating work among programmers, but it can be used to track changes in any set of files. Its goals include speed, data integrity, and support for distributed, non-linear workflows.

Git work flow

Git work flow

How to install Git in linux/Ubuntu?

To install git just run the below commands in the terminal

sudo apt update
sudo apt install git
git --version

git --version will give you the installed version of git.

How to configure the Git?

The following commands will set your git commit username and email address:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

We can verify the config with command git config --list

What is "git init"?

The git init command creates a new Git repository. It can be used to convert an existing, unversioned project to a Git repository or initialize a new, empty repository.

Create a directory that you want to work and cd to it and execute the below command

git init

Above command will create a directory .git which will contain all git revision history.

What is "git remote"?

  • git remote allows us to manage the connections of remote repository.
  • It allows us to add new remote connections
  • It shows existing remote connections
  • It allows us to remove the existing connections

Add new git remote connection

git remote add <short name> <url>

Example:

git remote add origin https://github.com/AnjaneyuluBatta505/learnbatta.git

Show existing git remote connections

git remote -v

Output:

origin  https://github.com/AnjaneyuluBatta505/learnbatta.git (fetch)
origin  https://github.com/AnjaneyuluBatta505/learnbatta.git (push)

Remove existing remote connection

git remote remove <short name>

Example:

git remote remove origin

what is "git fetch" ?

  • git fetch really only downloads new data from a remote repository - but it doesn't integrate any of this new data into your working files. Fetch is great for getting a fresh view on all the things that happened in a remote repository.

What is "git branch"?

Git branch is a way to write the new code without making changes to the existing code. Whenever we develop a new feature then we will create a new branch from the stable branch and implement the feature. After getting approval we will merge the branch back to the stable branch.

  • In most of the code repositories the stable branch name is master. We can change the stable branch to any name.

what is "git checkout "?

  • With git fetch command we will get the new data from the remote repo.
  • Let's change the branch to stable branch master with below command.
git checkout master
  • We can give any branch name instead of master.

how to create a new git branch from branch master?

  • If you want to create a new branch feature1 from stable branch master then run the below commands.

git branch
Outout:

*master
  • Let's create the branch
git checkout -b feature1

Above command will create a new branch feature1 from branch master.

Output:

Switched to a new branch 'feature1'

Let's see what branch we are on

git branch

output:

* feature1
  master

What is "git status"?

  • The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven't, and which files aren't being tracked by Git. Status output does not show you any information regarding the committed project history.

  • Let's say, I have added a new file that looks like below

hello.py

print("hello world")
now run the command git status. We will get the below output

On branch feature1
Untracked files:
  (use "git add <file>..." to include in what will be committed)
    hello.py

nothing added to commit but untracked files present (use "git add" to track)

What is "git add"?

  • The git add is a command, which adds changes in the working directory to the staging area. With the help of this command, you tell Git that you want to add updates to a certain file in the next commit. But in order to record changes, you need to run git commit too.

  • Let's add file hello.py to staging area with below command

git add hello.py
  • Let's see the status
git status

Output

On branch feature1
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    new file:   hello.py

What is "git commit"?

  • The commit command is used to save your changes to the local repository. Note that you have to explicitly tell Git which changes you want to include in a commit before running the "git commit" command. This means that a file won't be automatically included in the next commit just because it was changed.

  • Let's commit the file

git commit -m "hello.py file added"

Output:

[feature1 c1e6c1e] hello.py file added
 1 file changed, 1 insertion(+)
 create mode 100644 hello.py

What is "git push"?

  • The git push command is used to upload local repository content to a remote repository. Pushing is how you transfer commits from your local repository to a remote repo. ... Remote branches are configured using the git remote command. Pushing has the potential to overwrite changes, caution should be taken when pushing.

  • Currently, all changes that we committed are in local branch we need to push the branch to remote repository. Let's do that with command git push <branch name>.

git push origin feature1

What is "git pull"?

  • We are working in a team, we work with multiple features. If we have dependency on some feature we need to get that code to our local environment. To get the code from the remote repository to local we use git pull.
git pull

What is "git diff"?

  • Git diff allows to see that changes that we made to the code.
  • git diff is a multi-use Git command that when executed runs a diff function on Git data sources. These data sources can be commits, branches, files and more.
git diff

how to diff a single file with remote branch file?

git diff origin/<branch_name>:path/to/file.ext path/to/file.ext