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¶
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
Above command will create a directory
.git which will contain all git revision history.
What is "git remote"?¶
git remoteallows 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>
git remote add origin https://github.com/AnjaneyuluBatta505/learnbatta.git
Show existing git remote connections¶
git remote -v
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>
git remote remove origin
what is "git fetch" ?¶
git fetchreally 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"?¶
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
git fetchcommand we will get the new data from the remote repo.
- Let's change the branch to stable branch
masterwith below command.
git checkout master
- We can give any branch name instead of
how to create a new git branch from branch master?¶
- If you want to create a new branch
feature1from stable branch
masterthen run the below commands.
- Let's create the branch
git checkout -b feature1
Above command will create a new branch
feature1 from branch
Switched to a new branch 'feature1'
Let's see what branch we are on
* 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
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.pyto staging area with below command
git add hello.py
- Let's see the status
On branch feature1 Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: hello.py
What is "git commit"?¶
commitcommand 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"
[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
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.