Skip to content

hediyeorhan/GitCommandsDocumentation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 

Repository files navigation

GIT COMMANDS

Git is a version control system. It allows us to access old and new versions of the code we are working on. It allows us to check what changes have been made in the project and by whom. Let's now examine the common commands used in the git versioning system.

Email and username configuration must be done before starting to operate in git system.

  git config --global user.name "hediyeorhan"
  git config --global user.email hediyeorhan2015@gmail.com # please add your username and e-mail address here.

First, we create a git folder in the project we will work on in our local directory with the command given below.

  git init # initilazition

Then, if we want to add files to the project, we can add files with the touch command.

  touch add_file # create file
  mkdir folder # create folder

We use the git add command to make these and similar changes permanent in the project. The Add command loads all files into the transition zone.

  git add . # adds all files
  git add add_file # adds the file we specifically specified

After adding the files, a commit message should be written to these files.

  git commit -m "commit message"

To run the add and commit commands on a single line, use :

  git commit -am "commit message"

The rm command deletes files in the directory, the mv command changes the file name. It is used to move files.

If we want to get the files back after adding them with git add command:

  git reset HEAD filename
  git checkout --filename

In Git, the status command is used to check if there is a change in the project that has not been committed.

  git status # shows current git status. It shows if the current directory is linked to any git.

The log command is used to see by whom and when commit messages were made.

  git log

To return to the old commit version, you can list the commits with the log command and then return to that commit by obtaining the commit hash code.

Hash code: Each commit has its own number.

  git log
  git checkout version_hash_code
  git commit -m "old version copied"

A .gitignore file should be created to hide the files in our local directory that we do not want to upload to github. Then we write the files we want to hide into .gitignore.

  touch .gitignore # you can find .gitignore templates on Google according to the technology you use.

If the files we added to our .gitignore file are not hidden and are being uploaded to github, we can use :

  git rm --cached file_name
  git commit -m "cache cleared"

"Branch" are independent workspaces that represent different development paths of the project. Branches are used to track and test different features or enhancements of the project, or to allow different teams to work on different tasks.

Head indicates / points to which branch and which commit we are on.

Create new branch:

  git branch new_branch_name

Branch switching :

  git switch branch_name

Merge two branches :

  git switch branch1   # we went into branch1 and merged it with branch2.
  git merge branch2

List current branches

  git branch # it indicates which branch we are in with '*'.

Files / folders created in each branch belong to that branch. If not merge, they cannot be accessed by other branches. Commits from different branches are also not visible to each other.

Fast forwarding: It is the process of merge the changes made in a branch derived / created from the master branch to the master.

When there are conflicting codes in both branches: when the other branch is merged while in the master branch, the changes in the other branch are written to the file in the master branch.

When a file deleted in the master is modified in the other branch and merge is desired, the merge conflic situation arises because there is no file in the master.

!!! To merge any two branches, there must be no overlap.

To restore the file from the last commit :

  git restore filename

Git can store changes that are not added and committed on itself with stash. When done in this way, the changes are not visible in the 'git status' command. Git stores it inside itself and we can retrieve it whenever we want.

  git stash  # hiding the file that was not committed.

To get the information stored in git stash back into files in our project :

  git stash pop  # it brings it back and deletes it from the list.

When different changes are made in a row, all stashes are listed and kept in ids.

  git stash list  # it shows what we keep up to date.

If we want to add stashes one by one :

  git stash apply stash_id  # it does not delete it from the list.

Clear all stash :

  git stash clear

Returning to past commits

To return to an old commit :

  git log # from here, the hash code of the desired commit can be obtained.
  git checkout commit_hash_code

When returning to an old commit, the 'HEAD detached' branch is switched to instead of the master branch. You can go back to the master branch with the git switch command. Or a new branch can be created and the edits of the old commit can be kept here.

To return to the commit only one step before :

  git reset HEAD~1

The reset command can be used to delete all commits after the specified commit from the git log history. However, this command only deletes commits from the commit history and does not delete the changes made to the commits deleted in the project.

  git reset commit_hash_code

Changes made to the project by commits deleted with the hard parameter can also be deleted.

  git reset --hard commit_hash_code

The revert command is used to keep commits in the commit history and only delete changes from the project. It undoes the actions and creates a new commit. It does not delete commits in the git log history.

  git revert commit_hash_code  # the project gets this commit back.

To see changes/differences between two commits or in a project, use :

  git diff HEAD # what shows differently according to the last commit.

  git diff commit1_hash_code commit2_hash_code # it shows the difference between two commits.

  git diff commit1_hash_code : commit2_hash_code # it can also be used this way.

To see the difference between the two branches :

  git diff branch1_name branch2_name

  git diff branch1_name : branch2_name

To clear the logs and rewrite the date and get rid of merge commits in between :

  git switch branch1_name
  git rebase branch2_name  # first the commits in branch2 are sorted, then the commits in branch1 are sorted.

Uploading the project to github

To upload our local project to Github, a github repo must first be created. Afterwards, we can check the updates of the additions and commits we made in the project with git status. The remote command should be used to remotely connect to the repository we created.

  git remote add origin github_repo_url.git # the term "origin" represents the repo url for easier use in later commands. 

After the remote connection is established, the push command is used to upload our local files to github.

  git push -u origin main # if your branch name is different, it can be changed. For example: master, feat .. 

Here, when the push command is executed, you are asked to log in to your github account from the terminal screen or browser.

If main / master branch will be pushed, only git push command will be enough. If a different branch is to be pushed, the git push origin branch_name command should be used.

PULL REQUEST

It is a request to modify a repo that belongs to someone else. Or someone else's request to modify a repo that belongs to me.

In order to see the changes we made in the project on Github on local, we need to pull the project to local. Branches on Github are considered remote branches. To list these branches, you can use :

  git branch -r 

To pull the final version of the project from github to local :

  git fetch # can be used when merge is not desired.

To navigate between remote branches, use :

  git checkout origin/branch_name

To merge the current version of the project that we have localized from Github :

  git pull 

git pull = git fetch + git merge

push -> sends to Github

pull -> pulls from Github.

To pull a repo that belongs to someone else and make changes to it, we need to fork that repo on Github so that it is listed among our own repos. Then we should clone it to our local repository by getting the url address among our own repos.

  git clone repo_url.git # move repo to local.

This way we can push and pull this repo.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published