Check version
git -v
Initial Configurations
On a console window:
git config --global user.name "Gourab Sarkar"
git config --global user.email "gourab.sarkar01@outlook.com"
git config --list
git config --listto see all your configurations
There are a lot of configuration items available.
git config --globalfor User level configurations (recommended).git config --systemfor System level configurations across Users.git configfor Project level configurations.
So, if git configuration needs to be created / changed for a particular project on disk, use the following commands.
git config user.name "Gourab Sarkar"
git config user.email "gourab.sarkar01@outlook.com"
git config --list
Git Help
Getting help from Git is easy. Type:
git helpfor all help commands at once.git help logfor help on git logging. Page opens in a browser.git help initfor help on initializing a repository. Page opens in a browser.
The .git folder
Inside a directory which has been already initialized as a Git repository, a hidden directory called .git can be found. This is where git stores all configs and tracking for that repository (or project).
Deleting this directory will completely remove Git and any of its tracking from that repository (or project).
List statistics for all files changed in a commit
To see statistics for the list of files which changed in the latest (HEAD) commit, use:
git show --stat --oneline HEAD
Remove the --oneline switch to see detailed changes, similar to how git log and git log --oneline works.
To see the list of changed files in a specific commit, use:
git show --stat --oneline 5321a8d
where 5321a8d is the commit ID of the specific commit for which you want to see the list of changed files.
Describing a commit
To describe a commit, i.e. to see the diff of all files changed in a commit, use:
git show --oneline HEAD
OR
git show --oneline <COMMIT_SHA>
Multi-line commit messages
- To write out a multi-line commit message, just use:
git commit
and press Enter. Because a commit must have its message, Git will open the default text editor registered with git, where multiple lines can be typed out. To finalize, save and close the file.
Once the file is closed, the commit is automatically made.
The Commit Log
Displays the commit history with the topmost one being the most recent.
git logshows all commits.git log -n 5shows the 5 most recent commits.git log --since=2020-01-01shows all commits made on or after Jan 1, 2020.git log --until=2020-06-16shows all commits made until June 16, 2020.git log --author="Gourab"shows all commits made by any user whose namestrings have the substring or string “Gourab”.git log --grep="Init"shows all commits that have the substring “Init” in their commit messages.grepstands forGlobally search for Regular ExPressions.
git log --onelineshows a condensed, single-line version of the commit log.
Branches
There can be multiple branches in a repo, but the default branch is master.
Listing branches
git branch
This command lists all branches and highlights the currently selected branch.
Switch to an existing branch
git checkout my-existing-branch
This command will switch the currently selected branch (default: master) to my-existing-branch.
Renaming current branch
git branch -m new-branch-name
Renaming a different branch
git branch -m old-branch-name new-branch-name
To delete the older pushed branch, use:
git push origin --delete old-branch-name
Create a new branch based off of an existing branch
git checkout -b FEATURE
This command creates a new branch called FEATURE which is based off of the currently selected branch (usually master by default), then does checkout on it, thus switching your current working branch to FEATURE.
To create a new branch based off of another branch (let’s say anotherBranch):
git checkout -b newFeatureBranch anotherBranch
Deleting an existing local branch
git branch -d branch_name
The -d option is an alias for --delete, which only deletes the branch if it has already been fully merged in its upstream branch.
Dangerous:
git branch -D branch_name
The -D option is an alias for --delete --force, which deletes the branch irrespective of its merged status.
Merge branches
To merge branch MySecondFeature into another branch MyFirstFeature:
git checkout MyFirstFeature
git pull
git merge MySecondFeature
git merge MySecondFeature merges the branch MySecondFeature into the current working directory branch MyFirstFeature.
To abort a merge: git merge --abort.
Update upstream branch and merge into current branch
The branch feature is created and checked out from main. To pull updates done on upstream branch main and merge changes into downstream feature branch:
git pull origin main
Status of a repository
git status
Removing tracked files
To stop tracking a file you need to remove it from the index. This can be achieved with this command.
git rm --cached <file>
If you want to remove a whole folder, you need to remove all files in it recursively.
git rm -r --cached <folder>
The removal of the file from the head revision will happen on the next commit.
Note: While this will not remove the physical file from your local, it will remove the files from other developers machines on next git pull.
The Three Trees in Git
Git has a three tree architecture:
HEAD-> Last commit snapshot, next commit’s parentStaging Index-> Proposed next commit snapshotWorking Directory-> Sandbox
Tracking changes to files in the repository with git diff
git diffshows changes between the staging tree and the working directory.git diff --stagedshows changes between the repository and the staging tree.
Compare multiple commits
Multiple commits can be compared in Git. The syntax is:
git diff old_commit_SHA..new_commit_SHA --color-words
git diff 34d1bbc..f55ba2b --color-words
git diff 34d1bbc..HEAD --color-words
This shows all changes in the 2 commits merged into a single diff.
Undo changes in the Working Directory
- To undo changes in the working directory, use
git restore:
git restore explorers.html
- To undo all changes at once from the working directory and replace them with copies of files in the repository, use:
git restore .
The . indicates all changes to all files should be undone.
Undo Staged changes
To undo changes that have already been staged, use:
git restore --staged tours.html
git restore --staged directory/
To undo all staged changes in current working directory, use:
git restore --staged .
Undo committed changes
To undo commits as well as discard changes from the working directory,
git reset --hard HEAD~1
Here HEAD~1 means that the working directory will be restored to the state of 1 commit before the current HEAD. Similarly, HEAD~2 will undo the last 2 commits and discard their changes.
To undo commits but keep changes in the working directory so a better commit can be made,
git reset HEAD~1
Undo pushed commits (Revert commits)
- To revert a commit and therefore undo all changes in that commit, use:
git revert f70006a
where f70006a is the partial SHA ID of the commit that is to be reverted. The commit message still needs to be supplied to this new commit which opens in the default text editor registered with Git.
- To revert a range of commits and create a new commit with the reverted changes,
git revert <oldest_commit_hash>..<latest_commit_hash>
Note that the latest_commit_hash is not included in revert.
- You might also need to do a
git revert --continuein case of conflicts after resolving them.
Amending commits
The latest commit (HEAD commit) can be “amended”. This means that changes in that commit can be changed, and so can be commit metadata like the commit message. Here’s how to do it:
git commit --amend -m "Amended commit"
The SHA of the HEAD commit will change after amending a commit.
Technically, what amend does is that it takes whatever was in the HEAD commit, bring it back down to staging, add whatever changes staging has and then recommit the commit again. This generates a new SHA for the commit as the metadata (or actual data content) for the commit has changed.
Cherry picking commits
To cherry pick a commit, use:
git cherry-pick commitSHA
Cherry pick multiple specific commits
git cherry-pick commitsha1 commitsha2 commitsha3 commitsha4 commitsha5
In the above case, note that you can cherry-pick any number of commit hashes at once, and in any order you want. They will simply be applied one-at-a-time, and in the order you specify. If any conflicts arise, you will have to resolve them one-at-a-time then use git add my_file then git cherry-pick --continue when done to continue the cherry-pick process.
Cherry pick a range of commits
Notice that to cherry-pick a range of commits, you must specify a starting and ending commit hash, with .. between them. However, in a range of commits, the beginning commit is NOT included. Therefore, to include it, you must specify the commit before the beginning commit. The syntax to specify the preceding commit is to put ~, ~1, or ^ right after your commit, as in: beginning_commit~, which means: “the commit right before beginning_commit”.
# A. INCLUDING the beginning_commit
git cherry-pick beginning_commit~..ending_commit
# OR (same as above)
git cherry-pick beginning_commit~1..ending_commit
# OR (same as above)
git cherry-pick beginning_commit^..ending_commit
# B. NOT including the beginning_commit
git cherry-pick beginning_commit..ending_commit
To specify three commits prior to beginning_commit, you can do this:
beginning_commit~3
Specialized stash and pop
Add only inventory-core/src/main/java/com/dell/sae/inventory/core/handlers/DeviceDiscoveryHandler.java to the stash.
git stash push inventory-core/src/main/java/com/dell/sae/inventory/core/handlers/DeviceDiscoveryHandler.java
Perform your pull or merge
git pull
List all stashes
git stash list
Show the diff of the most recent stash
git stash show -p stash@{0}
Apply the most recent stash without removing it
git stash apply stash@{0}
(Optional) Drop the stash after applying it
git stash drop stash@{0}
(Optional) Clear all stashes (use with caution!)
git stash clear
Remove untracked files from the Working Directory
To remove untracked files from the working directory, use:
git clean -n
which would show a list of actions Git would perform on untracked files. But it would not actually remove those files. This is what’s called a dry run. To actually remove files, and this action cannot be undone, use:
git clean -f
Cloning from a local repository on disk
Use this command to clone a repo from another folder on disk. Note that this command can be only used with .git contents - such as contents exported by GitHub export.
git clone C:\Users\gourab\Downloads\github_export\repositories\GourabIX\lab.git
Cloning a specific branch of a remote repository
With this, you fetch all the branches in the repository, checkout to the one you specified, and the specific branch becomes the configured local branch for git push and git pull. But you still fetched all files from each branch.
git clone -b <branchname> <remote-repo-url>
View remote URL for repository
To see the remote URL for a repository, use the following command.
git remote -v
Change remote URL for repository
To change the remote URL for a repository, use the following command. new.git.url/repo must be a valid Git URL.
git remote set-url origin new.git.url/repo