GIT FILE STATUS LIFECYCLE
The Git file status lifecycle describes the different states a file can be in when it is being tracked by Git. There are four main states in the Git file status lifecycle:
- Untracked: A file is untracked when it is not yet being tracked by Git. Git is unaware of the file's existence.
- Modified: A file is considered modified when changes have been made to it since the last commit.
- Staged: A file is staged when it has been marked to be included in the next commit. The changes made to the file are included in the staging area.
- Committed: A file is committed when the changes made to it have been saved to the Git repository.
git status:
git add:
- git add <filename>: Stages the changes made to the specified file for the next commit.
- git add . or git add --all: Stages all changes made to tracked files in the current directory and its subdirectories. It also stages any new untracked files that have been added to the repository.
git commit:
- git commit: If you enter git commit without any additional options, Git will open your default text editor and allow you to enter a commit message.
- git commit -m "commit message": Allows you to specify the commit message directly on the command line.
- git commit -a: Automatically stages all modified and deleted files before creating the commit.
GIT CONFIGURATION
The git config command is used to configure various settings and preferences for Git. This command can be used to set Git configuration at different levels, including system, global, and local.
- Set user name and email: Git username and email are used to identify the author of a commit. Git uses the username and email associated with your Git configuration to record the author of the commit. Git hosting services (such as GitHub) may use your email address to verify your identity for security purposes.
git config --global user.name "karan51ngh"
git config --global user.email "
[email protected]"
git config --global core.editor nano
- Check current configuration:
git config --list
- To edit your global Git configuration file:
git config --global --edit
MANAGING GIT REPOSITORIES
Git repositories are used to store and manage your Git projects. A Git repository contains all the files and folders in your project, as well as the revision history of each file.
git clone
[email protected]:username/my-repo.git
git fetch origin
git pull origin
git push origin
ORIGIN, REMOTE AND UPSTREAM
- In Git, "origin" is a default name for the remote repository where the local repository was cloned from. When you clone a Git repository, the remote repository you clone from is automatically assigned the name "origin".
- You can use the name "origin" to refer to the remote repository when executing Git commands, such as pushing or pulling changes to and from the remote repository.
- git remote Allows you to manage the connections to remote Git repositories. With this command, you can list, add, rename, and remove remote repositories.
- List all the remote repositories connected to your local repository:
git remote -v
- Add a new remote repository with the name :
git remote add <name> <remote_url>
- Rename a remote repository from to .
git remote rename <oldname> <newname>
- Display information about a specific remote repository, including it's branch mappings and the last time the remote repository was fetched.
git remote show <name>
- Remove the connection to a remote repository with the name from your local Git repository.
git remote remove <name>
- In Git, upstream refers to the original repository that a forked repository was created from. When you fork a repository on GitHub, the original repository is automatically added as a remote called "upstream".
- You can use the upstream repository to synchronize your forked repository with any changes made to the original repository. This is done by fetching the changes from the upstream repository and merging them into your local repository.
git fetch upstream
git merge upstream/<branch_name>
- To set the upstream repository for your local branch, you can use the following command:
git push --set-upstream <remote> <branch>
GIT BRANCHING
Git branching is a feature that allows developers to work on different versions of a project simultaneously. You can create multiple branches, each with its own set of changes, and switch between them to work on different features or bug fixes. This helps to isolate changes and prevents conflicts between different parts of the project.
- Create a new branch:
- Switch to an existing branch:
- git checkout <branch-name>
- Create a new branch and switch to it:
- git checkout -b <branch-name>
- List all branches:
- Merge a branch into the current branch:
git merge <branch-name>
git branch -d <branch-name>
- Delete a branch (even if it has unmerged changes):
git branch -D <branch-name>
git branch -m <old-branch-name> <new-branch-name>
MISCELLANEOUS
git diff:
- By default, git diff shows the changes that are not yet staged.
- use --staged or --cached for seeing the changes that have already been staged.
- use the --color option to highlight the changes with colors.
git rm:
- git rm <filename>: Removes the file from the working directory and stages the removal of the file in the next commit.
- git rm --cached <filename>: Removes the file from the staging area, but not from the working directory.
- git rm -r <directory>: This removes a directory and its contents from the repository.
git mv: