· Hakan Çelik · Git · 4 dk okuma
Git Commands

Git Commands
We installed Git and configured our settings — now it’s time to learn Git commands.
Reminder
Configure user information for all local repositories:
git config --global user.name "name"Configure the email address for all local repositories:
git config --global user.email "email address"This information is stored by Git along with every change you make and its timestamp.
Init
Use the init command to create a new repository.
git initor
git init project-nameClone
Use the clone command to download a project and its entire version history from a remote Git server.
git clone remote_repo_urlExample for GitHub:
git clone https://github.com/{your_username}/{your_repo_name}.gitAdd
https://git-scm.com/docs/git-add
Moves the files you’ve changed to the staged area.
You can provide the first argument pathspec as a file path or glob:
git add foo/bar/file.exgit add *.pyBy writing it this way you can move all the changes you made in Python files to the staged area. If you give . as the pathspec argument, it takes all changed files.
git add -pShows you the diff of your changes and moves a change to the staged area after your confirmation.
Commit
https://git-scm.com/docs/git-commit
Saves the changes in the staged area together with a message.
git commit -m "your_commit_message"
If you want to write over the previous commit (actually it doesn’t overwrite — it creates a new commit):
git commit --amend
When you use the --amend argument without -m, it takes the previous commit message as is. If you use -m and enter a commit message, you theoretically edit the previous commit message.
git commit --amend -m "messages"
If you’re using git-hooks (or a technology that uses git-hooks, such as pre-commit):
The --no-verify argument lets you commit while skipping hooks.
Status
For a status check, this shows you your current state — from here you can tell whether you need to run add or commit next, and it also lists the changed files.
git statusLog
Used to list and review all changes made so far.
git logThe output for each commit includes:
- Who made the commit
- When it was made
- The commit message
- The commit hash value
Reset
Used to revert to an older version when something goes wrong. After git log, copy the commit hash value of the version you want to go back to and use reset to go to that version.
git reset commit_hash_valueUndoes all commits made after the given one, while preserving changes locally.
git reset --hard commit_hash_valueDiscards all history and reverts to the state of the specified commit.
Branch
Used to list branches, create new ones, and delete them.
List Branches
git branchCreate a New Branch
git branch new_branch_nameCreate a New Branch and Switch to It
git checkout -b new_branch_nameDelete a Branch
git branch -d branch_nameCheckout
Allows you to switch from one branch to another. If you’re on the master branch and have a branch called new_branch that you want to switch to:
git checkout new_branchMerge
If you want to merge the changes you made in new_branch with the master branch:
git checkout masterSwitched to master branch.
git merge new_branchMerged the changes from new_branch into master.
Or:
git pull origin masterYou can use this to pull the changes from the master branch into your current branch.
Pull
Fetches (synchronizes) changes from the repository on the remote server.
git pullor
git pull origin masterPush
Sends the changes you made locally (on your computer) to the repository on the remote server.
git pushIf your changes conflict with what’s on the remote server, Git will warn you that you need to pull before pushing. If you still want to send your changes — even if it deletes what’s on the remote server — you can use:
git push -fThe -f here stands for force.
Remote
https://git-scm.com/docs/git-remote
Lets you manage other repositories connected to your Git repo.
This means you can work connected to more than one Git repository — one of them can be the original repo and the other an upstream repo. The upstream repo is the one you forked from. This way you can pull new updates from the original repo into your own fork and continue developing with an up-to-date codebase.
Type git remote -v to see the names and addresses of the repositories connected to your Git repo.
git remote add {remote name} {repo address} — for example: git remote add upstream https://github.com/user/repo.git
git remote remove {remote name} to delete an added repository.
Rebase
https://git-scm.com/docs/git-rebase
Allows you to move a previously made commit to the top by editing the Git history.
git rebase moves the most recent commit you made on your current branch to the top. When might you use this? After a git pull that brings new commits into your repo, you can use it to move your unpushed changes to the top.
The Gitignore File
There may be some files we don’t want Git to track. We tell Git about these files using the .gitignore file.
There are gitignore files available for various programming languages at the address below. Depending on which language you use in your project, you can draw inspiration from this address to create your gitignore file and keep the files you don’t want Git to track under control.
Hakan Çelik

