Last active
March 28, 2024 12:20
-
-
Save krishnamurthydasari/64047f6e07bf9d71ce7b8a30f4835198 to your computer and use it in GitHub Desktop.
Git Notes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Creating new repository: | |
======================== | |
Create a new repository on GitHub by selecting initialize this repository with README. | |
Clone repository using below commands, | |
git clone https://github.com/krishnamurthydasari/TestProject.git | |
touch NewFile.txt | |
git add NewFile.txt | |
git status | |
git commit -m "Adding NewFile.txt" | |
git status | |
git push -u origin master | |
Adding an existing project to GitHub: | |
===================================== | |
Create a new repository on GitHub. To avoid errors, do not initialize the new repository with README, license, or gitignore files. | |
cd existing_folder | |
echo "# TestProject" >> README.md | |
Initialize the local directory as a Git repository. | |
git init | |
Add the files in your new local repository. This stages them for the first commit. To unstage a file, use 'git reset HEAD YOUR-FILE'. | |
git add README.md | |
Commit the files that you've staged in your local repository and prepares them to be pushed to a remote repository. | |
To remove this commit and modify the file, use 'git reset --soft HEAD~1' and commit and add the file again. | |
git commit -m "first commit" | |
At the top of your GitHub repository's Quick Setup page, click to copy the remote repository URL. | |
Set new remote, | |
git remote add origin https://github.com/krishnamurthydasari/TestProject.git | |
Verify the new remote URL | |
git remote -v | |
Push the changes in your local repository up to the remote repository you specified as the origin | |
git push -u origin master | |
Git global setup: | |
================= | |
git config --global user.name "username" | |
git config --global user.email "user email" | |
If you dont want to setup creds globally, just remove --global from above commands, | |
git config user.name "username" | |
git config user.email "user email" | |
Pull: | |
===== | |
If you want to update your local cloned repository with latest changes from remote repository, | |
git pull origin master (OR git pull origin branch) | |
Fetch and Pull: | |
=============== | |
fetch will download any changes from the remote* branch, updating only your repository data, but leaving your local* branch unchanged. | |
pull will perform a fetch and additionally merge the changes into your local branch. | |
Diff from local repository to Origin master: | |
============================================ | |
git fetch | |
git diff origin/master | |
- After git fetch: | |
git diff-tree --name-only origin/master master | |
git diff-tree origin/master master | |
-After git pull: | |
git diff-tree ORIG_HEAD.. | |
git diff-tree ORIG_HEAD.. --name-only | |
Branches: | |
========= | |
https://help.github.com/articles/creating-and-deleting-branches-within-your-repository/ | |
git clone https://github.com/krishnamurthydasari/project1.git -b <Name of Branch> | |
git branch - will show all branches | |
To change to different branch, | |
git checkout <Name of Branch> | |
To create branch from git bash, | |
https://github.com/Kunena/Kunena-Forum/wiki/Create-a-new-branch-with-git-and-manage-branches | |
Releases: | |
========= | |
https://help.github.com/articles/creating-releases/ | |
Merge branches: | |
=============== | |
(Merging branch in to Master) | |
git checkout master | |
git branch | |
git merge development | |
git push -u origin master | |
=============== | |
Notes from Doc: | |
=============== | |
=============== | |
Recording Changes to Repository: | |
-------------------------------- | |
Short Status: | |
git status -m #short form of status | |
[git@ip-10-0-0-230 project1]$ git status -s | |
MM 1 | |
M 2 | |
M test.txt | |
?? 12345 | |
There will be 2 columns. First column shows what is modified and staged. second column shows modified but not staged. From above example, file named "1" has been modified, staged and then modified. File "2" is modified but not staged. File named "test.txt" has been modified and staged. File named "12345" is a new file (?? refers to new file). | |
Ignoring files: | |
.gitignore | |
Viewing Your Staged and Unstaged Changes/Differences: | |
git diff #To see what you’ve changed but not yet staged | |
git diff --staged #To see what you’ve staged that will go into your next commit | |
git diff --cached #To see what you’ve staged so far (--staged and --cached are synonyms) | |
git difftool #Display differences in tool format like vimdiff | |
git difftool --tool-help | |
Committing Your Changes: | |
git commit -m "comment" | |
Skipping the Staging Area: | |
git commit -a -m 'added new benchmarks' | |
Removing Files: | |
To remove a file from Git, you have to remove it from your tracked files (more accurately, remove it | |
from your staging area) and then commit. The git rm command does that, and also removes the file | |
from your working directory so you don’t see it as an untracked file the next time around. | |
If you simply remove the file from your working directory, it shows up under the “Changes not staged | |
for commit” (that is, unstaged) area of your git status output: | |
git rm PROJECTS.md | |
git status | |
The next time you commit, the file will be gone and no longer tracked. If you modified the file and | |
added it to the staging area already, you must force the removal with the -f option. This is a safety | |
feature to prevent accidental removal of data that hasn’t yet been recorded in a snapshot and that | |
can’t be recovered from Git. | |
git rm -f PROJECTS.md | |
"git rm" and "git rm -f" will delete file from hard disk itself. Another useful thing you may want to do is to keep the file in your working tree but remove it from | |
your staging area. Below command will delete file from Git stagged/commited area but file will exist in working tree or Hard disk. | |
$ git rm --cached README | |
Moving Files: | |
$ git mv file_from file_to | |
$ git mv README.md README | |
However, this is equivalent to running something like this: | |
$ mv README.md README | |
$ git rm README.md | |
$ git add README | |
Viewing the Commit History: | |
--------------------------- | |
git log | |
By default, with no arguments, git log lists the commits made in that repository in reverse chronological order — that is, the most recent commits show up first. | |
git log -p | |
shows the difference (the patch output) introduced in each commit. You can also limit the number of log entries displayed, such as using -2 to | |
show only the last two entries. | |
git log -p 2 | |
git log --stat | |
to see some abbreviated stats for each commit. prints below each commit entry a list of modified files, how many files were changed, and how many lines in those files were added and removed. It also puts a summary of the information at the end. | |
git log --pretty=format:"%h - %an, %ar : %s" | |
Undoing Things: | |
--------------- | |
git commit --amend | |
As an example, if you commit and then realize you forgot to stage the changes in a file you wanted to add to this commit, you can do something like this: | |
$ git commit -m 'initial commit' | |
$ git add forgotten_file | |
$ git commit --amend | |
You end up with a single commit — the second commit replaces the results of the first. | |
Unstaging a Staged File: | |
----------------------- | |
git reset HEAD <filename> | |
Example, modified file called testfile | |
[git@ip-10-0-0-230 project1]$ vi testfile | |
[git@ip-10-0-0-230 project1]$ git status | |
On branch master | |
Changes not staged for commit: | |
(use "git add <file>..." to update what will be committed) | |
(use "git checkout -- <file>..." to discard changes in working directory) | |
modified: testfile | |
no changes added to commit (use "git add" and/or "git commit -a") | |
[git@ip-10-0-0-230 project1]$ git add testfile | |
[git@ip-10-0-0-230 project1]$ git status | |
On branch master | |
Changes to be committed: | |
(use "git reset HEAD <file>..." to unstage) | |
modified: testfile | |
[git@ip-10-0-0-230 project1]$ git reset HEAD testfile | |
Unstaged changes after reset: | |
M testfile | |
[git@ip-10-0-0-230 project1]$ git status | |
On branch master | |
Changes not staged for commit: | |
(use "git add <file>..." to update what will be committed) | |
(use "git checkout -- <file>..." to discard changes in working directory) | |
modified: testfile | |
no changes added to commit (use "git add" and/or "git commit -a") | |
Unmodifying a Modified File: | |
---------------------------- | |
[git@ip-10-0-0-230 project1]$ git status | |
On branch master | |
Changes not staged for commit: | |
(use "git add <file>..." to update what will be committed) | |
(use "git checkout -- <file>..." to discard changes in working directory) | |
modified: testfile | |
no changes added to commit (use "git add" and/or "git commit -a") | |
[git@ip-10-0-0-230 project1]$ git checkout -- testfile | |
[git@ip-10-0-0-230 project1]$ git status | |
On branch master | |
nothing to commit, working tree clean | |
[git@ip-10-0-0-230 project1]$ | |
Working with Remotes: | |
--------------------- | |
git clone [email protected]:/home/git/project1 | |
git remote | |
git remote -v | |
origin — that is the default name Git gives to the server you cloned from. -v option will give all details | |
Tags: | |
===== | |
A tag/Release represents a version of a particular branch at a moment in time. It can not be updated or make commits. | |
$ git tag | |
$ git tag -l "v1.8.5*" | |
Git supports two types of tags: lightweight and annotated. | |
Annotated tag: | |
$ git tag -a v1.4 -m "my version 1.4" | |
lightweight tag: | |
$ git tag v1.4-lw | |
$ git tag | |
$ git show v1.0 | |
Tagging Later | |
$ git log --pretty=oneline | |
$ git tag -a v1.2 9fceb02 | |
Sharing Tags: | |
By default, the git push command doesn’t transfer tags to remote servers. You will have to explicitly push tags to a shared server after you have created them. This process is just like sharing remote branches — you can run git push origin <tagname>. | |
$ git push origin v1.5 | |
$ git push origin --tags --> if you have multiple tags | |
Checking out Tags: | |
$ git checkout 2.0.0 | |
$ git checkout -b version2 v2.0.0 | |
Git Aliases: | |
$ git config --global alias.co checkout | |
$ git config --global alias.br branch | |
$ git config --global alias.ci commit | |
$ git config --global alias.st status | |
Branches: | |
========= | |
A branch represents a separate thread of development that may run concurrently with other development efforts on the same code base without changing main line (master). Changes to a branch may eventually be merged back into another branch to unify them. | |
Create new branch | |
$ git branch <branch name> | |
Check out branch | |
$ git checkout <branch name> | |
To create a new branch and switch to it at the same time, you can run the git checkout command with the -b switch: | |
$ git checkout -b iss53 | |
Basic Branching and Merging: | |
--------------------------- | |
This is shorthand for: | |
$ git branch iss53 | |
$ git checkout iss53 | |
This is an important point to remember: when you | |
switch branches, Git resets your working directory to look like it did the last time you committed on | |
that branch. It adds, removes, and modifies files automatically to make sure your working copy is what | |
the branch looked like on your last commit to it. | |
Merging branch: | |
============== | |
Create branch hotfix from master, | |
$ git checkout -b hotfix | |
Switched to a new branch 'hotfix' | |
$ vim index.html | |
$ git commit -a -m 'fixed the broken email address' | |
[hotfix 1fb7853] fixed the broken email address | |
1 file changed, 2 insertions(+) | |
You can run your tests, make sure the hotfix is what you want, and finally merge the hotfix branch back into your master branch to deploy to production. You do this with the git merge command: | |
$ git checkout master | |
$ git merge hotfix | |
Updating f42c576..3a0874c | |
Fast-forward | |
index.html | 2 ++ | |
1 file changed, 2 insertions(+) | |
Delete branch: | |
-------------- | |
$ git branch -d hotfix | |
Merge Conflict: | |
--------------- | |
If you changed the same part of the same file differently in the two branches you’re merging together, Git won’t be able to merge them cleanly. If your fix for issue #53 modified the same part of a file as the hotfix branch, you’ll get a merge conflict that looks something like this: | |
$ git merge iss53 | |
Auto-merging index.html | |
CONFLICT (content): Merge conflict in index.html | |
Automatic merge failed; fix conflicts and then commit the result. | |
git status will give you details on conflict. | |
fix the file manually by comparing both versions and commit. After fixing file run git commit, | |
git commit | |
Branch Management: | |
================== | |
list all branches, | |
$ git branch | |
iss53 | |
* master | |
testing | |
* it indicates the branch that you currently have checked out (i.e., the branch that HEAD points to). | |
To see the last commit on each branch, you can run git branch -v: | |
$ git branch -v | |
iss53 93b412c fix javascript issue | |
* master 7a98805 Merge branch 'iss53' | |
testing 782fd34 add scott to the author list in the readmes | |
To see which branches are already merged into the branch you’re on, you can run git branch --merged: | |
$ git branch --merged | |
iss53 | |
* master | |
Because you already merged in iss53 earlier, you see it in your list. Branches on this list without the * in front of them are generally fine to delete with git branch -d; | |
To see all the branches that contain work you haven’t yet merged in, you can run git branch --no-merged: | |
$ git branch --no-merged | |
testing | |
Tag Vs Branch: | |
============== | |
A tag/Release represents a version of a particular branch at a moment in time. A branch represents a separate thread of development that may run concurrently with other development efforts on the same code base without changing main line (master). Changes to a branch may eventually be merged back into another branch to unify them. | |
Remote Branches: | |
================ | |
While the git fetch command will fetch down all the changes on the server that you don’t have yet, it will not modify your working directory at all. It will simply get the data for you and let you merge it yourself. However, there is a command called git pull which is essentially a git fetch immediately followed by a git merge in most cases. | |
git fetch | |
git pull --> this is equal to | |
git fetch | |
git merge | |
Command line instructions: | |
========================== | |
Create new project called "project1" from Webpage and then follow one of below options. | |
Git global setup | |
git config --global user.name "Krishnamurthy Dasari" | |
git config --global user.email "[email protected]" | |
Create a new repository | |
git clone http://ec2-34-214-101-151.us-west-2.compute.amazonaws.com/dasarik/project1.git | |
cd project1 | |
touch README.md | |
git add README.md | |
git commit -m "add README" | |
git push -u origin master | |
Existing folder | |
cd existing_folder | |
git init | |
git remote add origin http://ec2-34-214-101-151.us-west-2.compute.amazonaws.com/dasarik/project1.git | |
git add . | |
git commit -m "Initial commit" | |
git push -u origin master | |
Existing Git repository | |
cd existing_repo | |
git remote rename origin old-origin | |
git remote add origin http://ec2-34-214-101-151.us-west-2.compute.amazonaws.com/dasarik/project1.git | |
git push -u origin --all | |
git push -u origin --tags |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment