Skip to content

Instantly share code, notes, and snippets.

@IcedMonk
Last active September 1, 2023 11:10
Show Gist options
  • Save IcedMonk/01438b9aa6576afee9056aabe3f1c525 to your computer and use it in GitHub Desktop.
Save IcedMonk/01438b9aa6576afee9056aabe3f1c525 to your computer and use it in GitHub Desktop.
The Git and Github Commands with easy discriptions's to help you be a master in Git!

Git Mechanics N|Solid

git init To Initilise the workspace with git (To make it git repo in local computer)

git status To see the untracked files for git (Red: untracked, Green: Tracked)

git add <fileName> To let git know about the particular added file git add . To add all the files

git commit -m <message> To set the message for work done after adding to git

git push origin <branchName> To push the code to github branchName is basically master

git pull To get the data of the cloud to local computer

git log To get the Logs

git log --oneline To get the oneline only messages of commits

git checkout <Id> Will revert to that version, will get id with git log

git revert <id> Will bring back the old code with that id

it reset <id> will takes us to the id, but will not remove the added content(Use full to remove the extra or useless commits)

git reset <id> --hard Will reset us to the particular commit id, and delete the extra added content, to that specific time(basically take us back to the time)

Branches

git branch feature-1 Will create a branch of name feature1

git branch -a Will show all the branches, will color green for present working branch

git checkout feature-1 Will switch from master branch to feature-1 branch

git branch -d feature-1 will delete the feature-1 branch(SideNote- First switch to master branch)

git checkout -b feature-a Will create a branch named feature-a and also go into that branch

git merge feature-a Will merge the feature-a branch into the master branch(Do it from master branch)

In the case of conflict, we will need to fix the issue on master branch by deleting the comments and then git add . and then do git commit No need to give comment

git remote add origin https://github.com/repo We every time need to add the URL at git push, so we can add the Alias to the particular repo, so that it will have that url every time & then we can use this nice & short method to push the code to that repo/url git push origin master and so this will hold the url in origin and push to master git remote -v to check the alias on the particular repo

Conflict Resolution

In the case of a conflict, manually resolve the conflict in your code editor. Then run git add . and git commit.

git remote add origin <URL>

To set a remote repository URL. This will let you use short commands for pushing and pulling.

git remote -v

To view remote URLs for your repository.

git clone <URL>

To clone (download) a repository to your local machine.

git fetch

To download objects and refs from another repository without merging into your local branch.

git stash

To temporarily store changes you don't want to commit yet. You can apply them later.

git tag

To add tags to specific commits, usually for versioning.

git show <tag/commitId>

To show various types of objects, can be used to view the changes in a commit or tag.

Additional Tools

git config --global user.name "<Name>"

To set your username for all repositories on your system.

git config --global user.email "<email>"

To set your email for all repositories on your system.

git config --global core.editor "<editor>"

To set your preferred text editor for Git to use, such as Vim, Emacs, or VSCode.

git config --list

To list all Git configurations.

git config --global alias.<alias-name> "<git-command>"

To create a short alias for a Git command. For example, git config --global alias.cm "commit -m" will let you use git cm "message" instead of git commit -m "message".

git diff

To show changes between commits, or between the working directory and the index.

git diff --staged

To show changes that are staged (added to index but not yet committed).

git cherry-pick <commitId>

To apply changes from a specific commit to the current working branch.

git rebase <branchName>

To move or combine a sequence of commits to a new base commit. Useful for keeping feature branches up to date.

git bisect

To use binary search to find the commit that introduced a bug.

git blame <fileName>

To show what revision and author last modified each line of a file.

git reflog

To view a log of where your HEAD and branch references have been.

git stash list

To list all stashed changesets.

git stash apply

To reapply previously stashed changes.

git stash drop

To remove a stashed changeset.

git clean -n

To do a dry run and see what files would be removed.

git clean -f

To remove untracked files (use with caution).

git fsck

To check the connectivity and validity of objects in the database.

git gc

To clean up unnecessary files and optimize the local repository.

git archive

To create a tar or zip file including the contents of a single tree from your repository.

git submodule add <repository> <path>

To add a Git repository as a submodule at a specific path.

git submodule update --init --recursive

To initialize, fetch, and checkout any nested submodules for the repository.

Made By

IcedMonk

Stay Hungry😎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment