Skip to content

Commit b59ad03

Browse files
authored
added files
0 parents  commit b59ad03

12 files changed

+175
-0
lines changed

1-intro.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Configure Git
2+
git config --global user.name "xyz"
3+
git config --global user.email "xyz@gmail.com"
4+
git config --global --edit
5+
6+
Initialize Git
7+
git init
8+
9+
git status // check the status of all files.
10+
11+
Files in Git Repository Folder can be in one of 2 states:
12+
1. Tracked: Files that Git knows about and are added to repository.
13+
2. Untracked: files that are in working directory, but not added to repository.
14+
15+
16+
Git = Working directory(or working tree) + Staging index + Repository
17+
18+
19+
20+
// help
21+
git <command-name> -help // see all available options for specific command
22+
git <command-name> --help // opens Git Manual page for specific command
23+
git help --all // list all possible commands
24+
25+
26+
Use Shift+G to jump to the end of the list.
27+
Use q to exit the view.

branch.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
git branch xyz // will create new branch xyz
2+
git branch // will list all non-remote branches
3+
git branch -r // will list all remote branches
4+
git branch --all // will list all branches (non-remote + remote)
5+
git branch -d xyz // will delete branch (you have to be in other branch)
6+
7+
git checkout xyz // switch to branch xyz
8+
git checkout -b xyz // causes a new branch to be created and then checked out.
9+
git merge xyz // will merge xyz branch into current branch.

checkout.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Main function: Switch branches or restore working tree files.
2+
3+
git checkout <branch>
4+
// HEAD now points to <branch>. Local modifications to the files in the working tree are kept, so that they can be committed to the <branch>.
5+
6+
git checkout -b <new_branch>
7+
// It causes a new branch to be created and then checked out, if it doesn't exist.
8+
// If it exists, then error occurs.
9+
10+
git checkout -B <new_branch>
11+
// <new_branch> is created if it doesn’t exist; otherwise, it is reset.
12+
// Eq. of: git branch -f <branch>; git checkout <branch>;
13+
14+
git checkout <commit>
15+
// Head gets detached to commit

commit.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Commit creates a checkpoint in repository.
2+
We should always include a message when we commit.
3+
4+
git commit -m "updated files"
5+
6+
git commit -a -m "updated files"
7+
// -a will tell the command to automatically stage and then commit files that have been
8+
// modified and deleted in working tree (by doing git add and git rm and then commit),
9+
// but new files you have not told Git about are not affected.

diff.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
git diff // show changes between working tree and staging index.
2+
git diff HEAD // show changes between working tree and last commit. (What we would be committing if we run git commit -a)
3+
git diff --cached // show changes between staging index and last commit. (What we would be committing if we run git commit)
4+
git diff --staged // same as above
5+
6+
git diff <commit1> <commit2> // show changes between the two commits
7+
git diff HEAD^ HEAD
8+
// compare the version before the last commit and the last commit
9+
// (same as git show)

log.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
To view the history of commits
2+
3+
git log
4+
git log <branch> // logs the commits for only <branch>
5+
git log -5 // limit the no. of commits
6+
git commit --oneline // condenses each commit to a single line
7+
git log -p // display full diff of each commit
8+
git log --graph // Draw a text-based graphical representation of the commit history on the left hand side of the output.
9+
git log --all --graph // Draw graph having commits of all branches.
10+
git log --all --oneline --graph // Draw graph having commits of all branches in one line each.
11+
git log --author="<pattern>" // Search for commits by a particular author.

remotes.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
3 types of branches:
2+
1. local branches (stored locally) (e.g. main)
3+
2. remote branches (stored at remote) (e.g. main)
4+
3. local copies of remote branches (stored at locally) (e.g. origin/main)
5+
6+
For example, if at the remote, we have main branch and x branch, then locally, along with local main branch and local x branch, we also have origin/main branch and origin/x branch.
7+
These branches (type 3) get updated at the time of pulling/pushing only and they convey the info. about latest commits pushed/pulled.
8+
9+
Suppose that currently the local main branch and remote main branch are at sync.
10+
Now, consider 3 cases:
11+
12+
Case 1: We make a commit on local main branch.
13+
In this case, the local main branch gets updated to point to the new commit.
14+
But, neither the local origin/main branch nor the remote main branch gets updated.
15+
However, when we push the main branch to origin, both the local origin/main branch and the remote main branch gets updated to point to the new commit.
16+
17+
18+
Case 2: The remote main branch gets a new commit.
19+
In this case, the remote main branch gets updated to point to new commit.
20+
But, neither the local main branch nor the local origin/main branch gets updated.
21+
However, when we pull the main branch from origin, both the local main branch and the local origin/main branch gets updated to point to the new commit.
22+
23+
24+
Case 3: We make a commit (say c1) on local main branch and we also make some other commit (say c2) on remote main branch.
25+
In this case, the local main branch gets updated to point to commit c1, the remote main branch gets updated to point to commit c2.
26+
But, local origin/main branch doesn't get updated.
27+
28+
Now, pushing or pulling require merging. It may cause merge conflicts which may be needed to be resolved.
29+
Hence, it is safer to use git fetch here.
30+
31+
First use [git fetch origin] command which will just update local copy of remote branch (i.e. origin/main). This means that local origin/main branch will point to commit c2.
32+
Now, the local main branch will point to commit c1, the local origin/main branch to commit c2, and the remote main branch to commit c2.
33+
Now, we require to merge local origin/main branch into local main branch.
34+
We can run merge command to merge here.
35+
Note that we could also run git pull command instead of fetch then merge, since fetching + merging is equivalent to pulling, but then if any merge conflicts arise, then we need to resolve the conflicts as well.
36+
git pull origin main = git fetch origin + git merge main origin/main
37+
38+
Now, our local main branch is pointing to merged commit (say c3) and local origin/main branch and remote main branch are pointing to commit c2.
39+
Now, we can push the main branch which will update both the local origin/main branch and remote main branch to point to commit c3.
40+
Now, all the 3 branches (local main, local origin/main and remote main branch) are at sync with each other.
41+
42+
43+

reset.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
git reset --soft <commit>
2+
// resets the current branch head to <commit>. Does not touch the staging index or the working tree at all.
3+
4+
git reset <commit> OR git reset --mixed <commit>
5+
// resets the current branch head to <commit>. Resets the index but not the working tree.
6+
7+
git reset --hard <commit>
8+
// resets the current branch head to <commit>. Resets the index and working tree.
9+
10+
<commit> defaults to HEAD in all forms.

restore.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Restore specified paths in the working tree with some contents from a restore source.
2+
3+
git restore --staged <file_name>
4+
// restore file in staging index to match last commit.
5+
// This is same as git reset
6+
7+
git restore <file_name>
8+
// restore file in working directory to match the staging index.
9+
10+
git restore --staged --worktree <file_name>
11+
// restore file in both the working tree and the index directly to match last commit.
12+
// This is same as git reset --hard
13+
14+
git restore --source <source> <file_name>
15+
// restore file in working directory to match the contents of source.
16+
// Ex. of source: 6ba66ca, Head~2, etc.
17+

rm.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Remove files from the staging index, or from both working tree and the index.
2+
It will remove only those files that are known to git.
3+
4+
git rm file.txt // remove file from both working tree and index (index should be up to date)
5+
git rm --cached file.txt // unstage and remove paths only from index.
6+

show.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
git show // to show the changes between last two commits
2+
// same as git show HEAD
3+
// same as git diff HEAD^ HEAD
4+
5+
git show <commit> // to show the changes between <commit> and its parent

staging.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Staged files are files that are ready to be committed to the repository.
2+
Staged files are added into staging index.
3+
The "staging index" holds a snapshot of the content of the working directory,
4+
and it is this snapshot that is taken as the contents of the next commit.
5+
Thus after making any changes to the working directory, and before running the commit command,
6+
you must use the add command to add any new or modified files to the index.
7+
8+
9+
10+
git add file.txt // To stage a file
11+
git add --all OR git add -A // To stage all files
12+
git add -i OR git add --interactive // Add modified contents in the working tree interactively to the staging index.
13+
14+

0 commit comments

Comments
 (0)