Professional Documents
Culture Documents
1.git init, git config --global user.email, git config --global user.name, git
status, git add, git commit
1. Create a directory named myrepo
3. We need to initialize the Git in the directory by using git init command
4. In the next step we need to configure our global user email and name by the following
commands:
Replace the email with your github email and user name with github username.
Now Git should show that "file1.txt" has been added to the staging area.
9. Commit "file1.txt" to Git with the message "new file"
2. To list the files tracked by the git we need to use the following command
4. Now need to know how many files are present use the following command
6. Now that we have created the file2.txt we need to verify the status of git
You have created and added another file to Git, committed it with a message, and
checked the file list using both the `ls` command and the `git ls-files` command.
3 git log , git log --oneline, history, git commit -a –m
This will display a detailed log of all the commits in your repository.
2. To view the git commit history in a concise format we use the following command
This will show a summarized version of the commit history, with each commit
represented by a single line.
This will display the contents of "file1.txt", including the newly added line.You have viewed
the Git commit history, command history, appended a new line to "file1.txt", added and
committed it to Git using the `-a` flag, checked the Git status, listed the files in the directory,
and viewed the content of "file1.txt".
4. git diff ,git diff –staged
1. Create a file named "file3.txt".
Now Git should show that "file3.txt" has been added to the staging area.
The status should now indicate that there are no changes to be committed.
10. To View the differences in "file3.txt" that are not yet staged
11. To view the differences in "file3.txt" that are staged (already added)
12. Add the changes in "file3.txt" to Git
15. View the differences in "file3.txt" that are staged (already added)
20. Commit the changes in "file3.txt" to Git with the message "updated file3 with new
lines".
We have created, modified, and committed changes to "file3.txt" in Git, and checked the
Git status and differences at various stages.
5 git rm, git rm –cached
This command removes "file3.txt" from both the Git repository and the file system.
3. Commit the deletion of "file3.txt" with the message "del the file3.txt"
The status should now indicate that there are no changes to be committed.
5. Remove "file2.txt" from the Git repository but keep the file in the file system
This command removes "file2.txt" from the Git repository but keeps it in the file system.
10. Commit the deletion of "file2.txt" from the Git repository only, with the message
"deleted from only local repo"
11. Check the Git status
12. List the files in the current directory with detailed information
We have removed "file3.txt" from Git and the file system, removed "file2.txt" from Git but
kept it in the file system, and checked the Git status, file list, and tracked files at various
stages.
6.gitignore
The main purpose of the .gitignore file is to prevent certain files or directories from
being accidentally committed to the repository. It helps to avoid cluttering the
repository with unnecessary or sensitive files.
Syntax: The .gitignore file uses simple pattern matching rules to specify files and
directories to be ignored. Each pattern should be on a separate line. Here are some examples
of pattern syntax:
file.txt: Ignores a specific file named file.txt.
*.txt: Ignores all files with the .txt extension.
directory/: Ignores an entire directory named directory.
!important.txt: Negates a previous pattern and includes important.txt even if it matches an
earlier ignore pattern.You can also use wildcards (* for any number of characters, ? for a
single character) and directory globbing (**/ to match any subdirectory).
Comments: Lines starting with # are treated as comments and are ignored by Git. You can
use comments to provide explanations or notes within the .gitignore file.
6. Commit the addition of the `.gitignore` file with the message "ignore file added"
The status should now indicate that there are no changes to be committed.
That's it! You have created the `.gitignore` file, added "file2.txt" to it, committed the file to
Git, and checked the Git status at various stages.
7. git revert
The `git log --oneline` command is used to display the commit history in a condensed format,
showing each commit as a single line with a short commit hash and commit message. Here's
an example command and its output:
Command:
git log –oneline
`git revert`: The `git revert` command is used to undo a specific commit by creating a new
commit that undoes the changes introduced by the specified commit. It is a safe way to undo
changes without modifying the commit history. Revert commits are used to record the fact
that a previous commit was undone.
8 git reset –hard
This command displays the commit history with each commit represented as a single line,
showing the abbreviated commit hash and commit message.
`git reset`: The `git reset` command is used to move the current branch pointer to a different
commit, effectively resetting the branch to a specific commit. It allows you to discard
commits and move the branch pointer, potentially removing commits from the commit
history. This operation modifies the commit history and should be used with caution.
3. Reset the repository and move the HEAD pointer back by 2 commits
This command resets the repository to the state of the commit two steps back from the current
HEAD position, discarding any changes made in the subsequent commits.
This command resets the repository to the specified commit, discarding any changes made in
the subsequent commits.
This will show the current status of your Git repository after the reset operations.
We have checked the Git status, viewed the commit history, performed `reset --hard`
operations, and checked the Git status again at different stages.
9 git branch, git checkout
1. git branch: The `git branch` command lists all the branches in your repository. Since you
just initialized the repository, there is only the `master` branch.
2. git branch b1 master . This indicates that the branch b1 is created based on the master
branch.
3. Now we need to again execute the command git branch .Here we need to observe that the
output will show 2 branches
4. Now we need to change the git branch from master to b1 , to do that we need to execute
the following command.
14. To know the commit history once again execute the following command
15. To know about the list of files in the current working directory execute the following
command
16. To know about the list of files tracked by git use the following command
17. Till now we are in the b1 branch to change the branch to master we use the following
command.
18. Now use the ls command to know about the files in the current working directory i.e
master branch
19. To know about the list of files tracked by git we use the following command.
21. Now execute the following command to know about the commit history
10 git merge
2. `ls`: This command lists the files and directories in the current directory. It is not a Git
command but a command used in the command-line interface (CLI) to list files.
3. `git merge b1 master`: This command merges the changes from branch `b1` into the
`master` branch. It integrates the commits from `b1` into `master` and creates a new merge
commit, if necessary.
4. `ls`: Running `ls` again lists the files and directories in the current directory. This
command is used to list files in the CLI and does not have any direct relation to Git.
5. `git log --oneline`: This command displays the commit history in a concise format. It
shows the commit hashes and commit messages on a single line.
6. `git log --oneline b1`: Adding the branch name (`b1` in this case) to the `git log --oneline`
command filters the commit history to only show the commits specific to the `b1` branch. It
displays the abbreviated commit hashes and commit messages for those commits.
Please note that the `git branch`, `git merge`, `git log`, and `ls` commands are executed in the
command-line interface (CLI) or terminal, while `ls` is a system command to list files and
directories in a directory, not specific to Git.
4. This command stages all changes in the repository, including the new `file4.txt` file
5. This command creates a new commit on the `master` branch with the message "file on
master".
8. To know about the files in current working directory use the following command.
10. We have already created a text file (file4.txt) in master branch , we also need to create the
same text file in b1 branch . To create the file we use the following command.
11. To append the text in file4.txt we need to use the following command.
12. Run the ls command to know about the files in the current working directory.
13. To stage all changes in the repository, including the modified `file4.txt` file we need to
use the following command.
14. The following command creates a new commit on the `b1` branch with the message "file
add on b1".
15. To switch back again to the master branch use the following command.
16. This command merges the changes from the `b1` branch into the `master` branch.
Here there is a conflict arising . The conflict needs to be modified manually . All we need to
do is we need to go to myrepo and go to file4.txt . So open the file4.txt and remove all the
lines or combine the two lines by removing the unnecessary symbols. Then save the file.
20. To display the contents of the `file4.txt` file, we use the following command.
22. To display the contents of file4.txt when we are in b1 branch we use the following
command.
when resolving conflicts, you may need to use a text editor or a merge tool to modify the
conflicting file and remove the conflict markers manually.
11 git stash, git stash list, git show stash , git stash pop stash, git stash apply
stash, git stash clear, git stash -p, git drop stash
1. To make changes for file1.txt i.e here we are appending a new line to file1.txt that we had
already created in the previous steps. To append the line we need to use the following
command.
4. To display the contents of file1.txt and file2.txt we use the following command
5. git stash: This command saves the changes in a new stash and reverts the working
directory to the state of the last commit.
6. After stashing, the changes made to `file1.txt` and `file2.txt` will no longer be visible in the
working directory.
To display the contents use the following command.
7. Check the git status
8. git stash list: This command lists all the stashes that have been created.
9. git show stash@{0}: This command displays the changes made in the most recent stash
(`stash@{0}`).
11. git stash pop stash@{0}: This command applies the most recent stash (`stash@{0}`) and
removes it from the stash list.
12. The changes made to `file1.txt` and `file2.txt` are reapplied to the working directory. To
see the changes applied execute the following command.
20. git stash -p: This command allows you to interactively select specific changes to stash.
21. Check the git status
24. Now execute the git stash list command to check if the new stash is added to the list
25. git show stash@{1}: This command displays the changes made in the second most recent
stash (`stash@{1}`).
26. Now execute the following command
28. The following command removes the most recent stash (`stash@{0}`) from the stash list.
31. The contents of `file1.txt` and `file2.txt` are shown without the changes made in the stash.
12 git rebase
3. git reset --hard: This command resets the current branch and working directory to the
state of a specific commit. With `--hard`, it discards any changes and moves the branch
pointer to the specified commit.
4. After the reset, this command shows the updated commit history, excluding the discarded
commits.
5. git branch b2 master: This command creates a new branch named `b2` starting from the
`master` branch.
8. The following command stages all changes in the repository, including the new `file3.txt`
file.
9. The below command creates a new commit on the `master` branch with the message "txt
file add to master".
13. To view the commit history of b2 branch use the following command.
14. Create a new file file4.txt
17. This command creates a new commit on the `b2` branch with the message "txt file add to
b2".
git rebase command may cause conflicts if there are conflicting changes between the
branches. In such cases, you will need to resolve the conflicts manually.