KEMBAR78
Essential Git and Github commands | PDF
Git Commands
1. Create a repository from scratch
● To create a new repository: ​git init
● To download a project to particular folder: ​git clone <link of the file> <rename with
new name of your choice>
● To determine status of the repository:​ ​git status
2. Review repository history
● To displays the commit id(SHA), Author, Date and Commit message: ​git log
● To scroll the commit history - ​j​(up), ​k​(down) and ​q​(to exit).
● To​ ​displays SHA followed by commit message only: ​git log --oneline
● To views the modified files: ​ ​git log --stat
● To view the changes in the file changes: ​git log -p​ Alternative:​ ​git diff
● To view both modified files and file changes:​ ​git log -p --stat
● To view file changes of a specific SHAid: ​git show SHAid
● To view modified files of a specific SHA:​ ​git show SHAid --stat
● To view specific commit: ​git log --stat SHA
● Quick way to check no. of commits and commit messages done by other authors in a
collaborative projects: ​git shortlog
● Just to check no. of commits done and the authors:​ ​git shortlog -s -n
● Filtering out commit history by author name: ​git log --author=<name of the
collaborator>
● Filtering commits by words: ​git log --grep=<name of the word(s) to be filtered>
3. Git Add
● To add specific file to the staging index: ​git add <name of the files followed by
spaces>
● To add all the files to staging index: ​git add .
● To add all the tracked files: ​git add -u
4. Git Commit
● To write commit messages in the text editor already configured: ​git commit
● To add direct commit message without open text editor: ​git commit -m <Commit message>​. To
check out commit message style: ​https://udacity.github.io/git-styleguide/
● To directly adding of files to the repository: ​git commit -a​. Skips adding of files to the staging
index.
1
● To add to the staging index as well as directly commit message without opening text editor: ​git
commit -a -m <Commit message>
5. Having Git to ignore file
1. Create a file name by following command: ​touch .gitignore
2. Now open this file in visual studio code:​ ​code .gitignore
3. Now write the following code:
DS_Store
Name ​of​ the file you want to ignore.extension ​of​ the file
or /Directories to be ignored
6. Tagging
● To indicate each version of the file: ​git tag -a v1.0​. Then write the message for the tap once
code editor opens.
● To check version of the tag on terminal: ​git tag
● To show the current branch and tag version: ​git log --decorate
● To delete tag version: ​git tag -d v1.0
● To add tag for a particular commit by including SHAid: ​git tag -a v1.0 <SHAid>
7. Branching
● To show current branch which is highlighted: ​git branch
● To create side branch: ​git branch <name of your choice>
● To switch from master branch to the side branch: ​git checkout <name of the created
side branch>
● To display current active branch along with SHA ids and commits only: ​git log --oneline
--decorate
● To delete a particular named branch: ​git branch -d <name of branch you want to
delete>
● To create a new branch and switch to it: ​git checkout -b <name of the branch>
● To create a new branch in the same location as that of the master branch and switches to it: ​git
checkout -b <name of the branch> master
● To displays all the branches present in the folder: ​git log --oneline --decorate --graph
--all
2
8. Merging
● To undo branch merging: ​git reset --hard HEAD^
● To merge a branch with master branch: ​git merge <name of branch to merge in>
9. Undoing changes
● To rectify commit at the current branch: ​git commit --amend
● To undo the changes made previously to the particular commit followed by adding a new
commit: ​ ​git revert <SHAid>
Github Commands
● To manage remote repositories and establishes connection between local and remote computer:
git remote add origin https://github.com/di37/my-travel-plans.git
● To verify remote repository is added correctly:​ ​git remote -v
● To send changes and commits from local repository to the remote repository: ​git push <name
of remote> <branch name>
● To retrieve all the updates from remote to local repository: ​git pull <name of remote>
<branch name>
● To retrieve all updates from remote to local repository with affecting the branches in the local
repo:​ ​git fetch <name of remote> <branch name>
Tip: whenever cloning somebody else’s work, it is good tip to create a side branch and work on that.
Also before committing, run ​git diff​ command to make sure that changes took place.
● To rename remote: ​git remote rename <previous name i.e. origin etc> <new
name>
3

Essential Git and Github commands

  • 1.
    Git Commands 1. Createa repository from scratch ● To create a new repository: ​git init ● To download a project to particular folder: ​git clone <link of the file> <rename with new name of your choice> ● To determine status of the repository:​ ​git status 2. Review repository history ● To displays the commit id(SHA), Author, Date and Commit message: ​git log ● To scroll the commit history - ​j​(up), ​k​(down) and ​q​(to exit). ● To​ ​displays SHA followed by commit message only: ​git log --oneline ● To views the modified files: ​ ​git log --stat ● To view the changes in the file changes: ​git log -p​ Alternative:​ ​git diff ● To view both modified files and file changes:​ ​git log -p --stat ● To view file changes of a specific SHAid: ​git show SHAid ● To view modified files of a specific SHA:​ ​git show SHAid --stat ● To view specific commit: ​git log --stat SHA ● Quick way to check no. of commits and commit messages done by other authors in a collaborative projects: ​git shortlog ● Just to check no. of commits done and the authors:​ ​git shortlog -s -n ● Filtering out commit history by author name: ​git log --author=<name of the collaborator> ● Filtering commits by words: ​git log --grep=<name of the word(s) to be filtered> 3. Git Add ● To add specific file to the staging index: ​git add <name of the files followed by spaces> ● To add all the files to staging index: ​git add . ● To add all the tracked files: ​git add -u 4. Git Commit ● To write commit messages in the text editor already configured: ​git commit ● To add direct commit message without open text editor: ​git commit -m <Commit message>​. To check out commit message style: ​https://udacity.github.io/git-styleguide/ ● To directly adding of files to the repository: ​git commit -a​. Skips adding of files to the staging index. 1
  • 2.
    ● To addto the staging index as well as directly commit message without opening text editor: ​git commit -a -m <Commit message> 5. Having Git to ignore file 1. Create a file name by following command: ​touch .gitignore 2. Now open this file in visual studio code:​ ​code .gitignore 3. Now write the following code: DS_Store Name ​of​ the file you want to ignore.extension ​of​ the file or /Directories to be ignored 6. Tagging ● To indicate each version of the file: ​git tag -a v1.0​. Then write the message for the tap once code editor opens. ● To check version of the tag on terminal: ​git tag ● To show the current branch and tag version: ​git log --decorate ● To delete tag version: ​git tag -d v1.0 ● To add tag for a particular commit by including SHAid: ​git tag -a v1.0 <SHAid> 7. Branching ● To show current branch which is highlighted: ​git branch ● To create side branch: ​git branch <name of your choice> ● To switch from master branch to the side branch: ​git checkout <name of the created side branch> ● To display current active branch along with SHA ids and commits only: ​git log --oneline --decorate ● To delete a particular named branch: ​git branch -d <name of branch you want to delete> ● To create a new branch and switch to it: ​git checkout -b <name of the branch> ● To create a new branch in the same location as that of the master branch and switches to it: ​git checkout -b <name of the branch> master ● To displays all the branches present in the folder: ​git log --oneline --decorate --graph --all 2
  • 3.
    8. Merging ● Toundo branch merging: ​git reset --hard HEAD^ ● To merge a branch with master branch: ​git merge <name of branch to merge in> 9. Undoing changes ● To rectify commit at the current branch: ​git commit --amend ● To undo the changes made previously to the particular commit followed by adding a new commit: ​ ​git revert <SHAid> Github Commands ● To manage remote repositories and establishes connection between local and remote computer: git remote add origin https://github.com/di37/my-travel-plans.git ● To verify remote repository is added correctly:​ ​git remote -v ● To send changes and commits from local repository to the remote repository: ​git push <name of remote> <branch name> ● To retrieve all the updates from remote to local repository: ​git pull <name of remote> <branch name> ● To retrieve all updates from remote to local repository with affecting the branches in the local repo:​ ​git fetch <name of remote> <branch name> Tip: whenever cloning somebody else’s work, it is good tip to create a side branch and work on that. Also before committing, run ​git diff​ command to make sure that changes took place. ● To rename remote: ​git remote rename <previous name i.e. origin etc> <new name> 3