BASICS OF GITHUB AND GIT COMMANDS
1. What is the difference between pushing and pulling?
Pushing: 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 repository. It has the potential to overwrite changes and should be done cautiously. After a local repository has been modified a push is executed to share the modifications with remote team members. git push <remote> <branch> command Push the specified branch to <remote>, along with all the necessary commits and internal objects. This creates a local branch in the destination repository.
Pulling: he git pull command is used to fetch and download content from a remote repository and immediately update the local repository to match the content. git pull <remote> <branch> The git pull command is a combination of two commands, git fetch and git merge. Firstly, git pull will execute a git fetch scoped to the local branch that Head (current commit) is pointed at. Once the content is downloaded, git pull will enter a merge workflow. A new merge commit will be created and HEAD updated to point at the new commit.
2. How to initialise a new git repository [Describe all the steps]?
To initialise a new git repository there are some git commands that you need to follow as given below:
- Step-1: Create a directory to contain the project.
- Step-2: Then go into the new directory.
- Step-3: To initialize the repository use command git init and it will create a new and empty Git repository in the current working directory.
- Step-4: Type git add to add the files. To add the README.md file to staging area use command git add README.md and check the files status. To add multiple files at one time to staging area using single command git add . and check status.
- Step-5: Then commit those files to your local git repositories using command git commit -m "first commit".
- Step-6: To add GitHub repository URL use command git remote add origin <url>
Now you can check your GitHub repository using command git remote -v
3. What is the use of git clone and how to use it?
Cloning is a process of creating an identical copy of a Git remote repository to the local machine. Cloning a repository from a remote server means downloading it to your computer so you can work on the project. git clone <url> command clones a repository that already exists on GitHub, including all of the files, branches, and commits. Following are the steps to clone a repository:
- Step-1: To clone a repository, go to the repository page which you want to clone. This can be done through the side column on your dashboard.
- Step-2: Press Clone or download button.
- Step-3: Copy the code(url) that appears after pressing the button.
- Step-4: Once done, open Git bash on your system.
- Step-5: Check the directories (or repositories) already created under this directory using ls command
- Step-6: Press the following command to clone the repository: git clone <url> , url here represents to the same url that we copied in the third step.
- Step-7: Confirm the cloning by listing the directories once again using the ls command which lists all the files and folder. Check in the local drive by navigating to it manually.
4. How to ignore some files/folders from pushing?
Ignored files are usually platform-specific files or automatically created files from the build systems. Ignored files are tracked in a special file named .gitignore that is checked in at the root of your repository. There is no explicit git ignore command: instead the .gitignore file must be edited and committed by hand when you have new files that you wish to ignore. .gitignore files contain patterns that are matched against file names in your repository to determine whether or not they should be ignored. .gitignore typically contains a listing of files and/or directories that are generated during the build process or at runtime. Entries in the .gitignore file may include names or paths pointing to:
- temporary resources e.g. caches, log files, compiled code, etc.
- local configuration files that should not be shared with other developers
- files containing secret information, such as login passwords, keys and credentials
If you have already added a file to your Git repository and now want to stop tracking it, you can remove it from the index: git rm --cached <file> This will remove the file from the repository and prevent further changes from being tracked by Git. The --cached option will make sure that the file is not physically deleted.
If you want to remove the file from both the index and local filesystem, omit the --cached option.
When recursively deleting files, use the -n option that will perform a “dry run” and show you what files will be deleted:
git rm -r -n directory
You can pass filenames on the command line using command git check-ignore will list the filenames that are ignored. The git status command with the --ignored option displays a list of all ignored files: git status --ignored
5. What do you mean by Branch?
A branch in Git is a way to keep developing and coding a new feature or modification to the software and still not affecting the main part of the project. It represents an independent line of development and serve as an abstraction for the edit/stage/commit process.
a. Which branch should be used to keep deployment-ready code?
The main branch or in other words the central branch should be used to keep the deployment-ready code.
b. Create a new branch called development from the main branch.
git branch development origin/main or
git checkout -b development origin/main
c. Checkout one more branch deployment from the previous branch.
git checkout -b deployment origin/development
d. Push different data into both branches.
git checkout development
git add <file>
git commit -m "commit on branch development"
git checkout deployment
git add <file>
git commit -m "commit on branch deployment"
e. Merge data from both branches to the main branch.
git checkout main
git merge development deployment
git branch -d deployment
git branch -d development