Git Commands - serdarulutas/MADD-Notes Wiki

Important

  • Use git status to see the project status
  • Use git branch to see which brances exists, and which branch you are on.

Create a git repo

When on the project folder, call the command below. This will create a repo on the working directory,

git init


Add files to git repo

git add <filename> , or

git add -A for all files


Exclude files from git repo

Create a hidden .gitignore file under the root directory and list all the files' relative paths. git will ignore each file listed in the .gitignore file


Commit changes to the repo directory

Call git commit -m "<custom text message . This will keep local repo up to date. But it will not push the changes to the remote repo for you.


Assign a remote repo to the local git file

This is how git knows where eventually to upload the file.

git remote add origin <git_repo_url>

call git remote -v command to verify the result.

If a wrong url is added, then make this call : git remote set-url origin <new url>


Push the code remote server

git push or,

git push origin master : origin represents the url, and master represents the branch we are uploading.


Download existing repo

git clone <git_url> <foldername>


Branches

  • git branch <new_branch_name> will create a new branch
  • git checkout <branch_name> will change the branch name and override the local code

Merging branches:

  1. Switch to master branch with git checkout master
  2. Call git merge <other_branch_name>

Check differences between branches: Call git diff <other_branch_name>

Delete a branch after merge Call git branch -d <branch_name>. : Do this after the merge is successfully completed.


Reverting Mistakes

git reset <tag>~1 -> This will reset the git to the desired commit

git checkout <hex_code> -- <filename> -> This will get the file from the given commit.


Some other commands

git config --list will list existing configuration

git config --global <property_name> <property_value> will update the global config file. example : git config --global user.name "serdar ulutas"

git branch --set-upstream-to=origin/master -> This will make sure the local master brach will track the remote master-branch on github.com

git fetch downloads the remote depository without overriding local depository.

git show HEAD~1 will display code changes in that commit

git diff HEAD~2 HEAD~7 will display the code difference

git annotate <filename> Annotate file lines with commit information

⚠️ **GitHub.com Fallback** ⚠️