I am writing this for not an absolute solution to most of the issues but to help in remembering git commands. I am uploading this to Github so that I can comeback and check the things for myself later on.
For me these commands are the most important and are basically a crash cheat sheet for most of the common commands used in git. If executed perfectly you won't be needing any other command for work.
git init
touch <filename>
atom <filename>
mv <previousname> <newname>
git add <filename> <filename>
or use a '.' to add all of the files in a folder
or use ‘*’ to add file of a certain type such as * .html will add all .html files in a directory
git commit -m "message"
-m is used for not opening a commit file in CLI
git status
git tracked files means they are added and read by git
untracked files means that they are not added or catered by git
git rm --cached <filename>
rm <filename>
git log
git checkout <commit code>
git revert --no-commit <commit code>..HEAD
Note: you will still have the history of recent updates even if you go back
git reset HEAD~2
This will move your HEAD back to 2 commits and will stage the committed files that were headed back.
touch .gitignore
used for ignoring some git files just add names of files inside this .gitignore file and they will vanish
git add -A
This will add every file including .gitignore for next commit
git checkout -b <branchname> or git branch <branchname>
git branch -D <branchname>
git branch
git checkout <branchname>
git merge <branch_name_other_than_master>
This will automatically create a commit to git as well If you want to merge into master branch -> make sure you are currently in master branch
git remote add origin master
Remote: here is the remote origin
Add: means we are adding something to a remote rep
Origin: is a name for the push, can be anything else
Master: is a <branch name>, can be any other branch as well
git remote add origin <url>
Remote: here is the remote origin
Add: means we are adding something to a remote rep
Origin: is a name for the push, can be anything else
Url: is the url of the reporsitory you want to add to your git bash
Note: For existing repositories first check git pull for checking the current status and then commit changes.
Make sure to do this after adding a remote repository to your local computer having different history as compare to your remote repository
git pull origin master --allow-unrelated-histories
git push -u origin master
- Go to any github repository for example.
- Tap on folk.
- Copy the link forked from your copy of the repository.
- Open terminal and type.
git clone <url>
- Add a new branch:
git checkout -b <branchname>
- Make changes, then commit.
- Push your changes.
- Your repository will automatically show a green button stating compare & pull request in the pull request section
- Afterwards use pull request to make a request.
You don't have to have a master branch, but you do have to have a "default" branch in any git repository. In a non-bare repository, this is the checked-out branch; in a bare repository, it just means it's the default branch checked out for clones.
This default branch is called HEAD
, and must always exist in a valid git repository. If you've removed the branch that HEAD
was pointing at, you can reset it to a valid branch with:
git symbolic-ref HEAD refs/heads/<new-main-branch>
The most secure way is to use the update-ref
command:
git update-ref -d HEAD
It will delete the named reference HEAD, so it will reset (softly, you will not lose your work) ALL your commits of your current branch. If what you want is to merge the first commit with the second one, you can use the rebase command:
git rebase -i --root
This is a branch with the same content but without any commit history, and commit your new content on it:
git checkout --orphan <new-branch-name>
Muhammad Kumail