2 posts

·Tech·DevOps

Git Command Cleanup

Full page →
DevOps

Basic commands

Other than ``git clone and ````git commit, I've summarized commands that may be used frequently.

  • git reset [file name] Unstaging files
  • git stash Temporarily saves the current changes to stash and reverts them to the original commit state.
  • Here, you can apply the latest saved history in stash with git stash apply. When applying a specific index, you can use ```git stash apply stash@{N}````.
  • You can also use ``git stash pop``` to remove the stash from the list after applying the latest one.
  • In addition, there are drop and list functions.
  • git remote show [remote repository] View detailed information about a specific remote repository
  • ```git bisect```` Binary search to find a specific commit where a bug occurs
  • git reset --hard [commit] (force) return to a specific commit
  • git cherry-pick [commit 1] [commit 2] Bring commits from another branch into the current branch
  • You can also import it using the branch name.
  • git clean -n Check untracked files
  • git switch -c Create and move branches

Clearing up confusing Reset, Revert, Rebase, and Undo

I used this opportunity to organize the related commands that I used every time I managed git log, but I was confused because I had some free time since it was a holiday. To summarize in one line, If you want to delete the previous one, you can use Reset, if you want to organize only this, you can use Revert, if you want to organize it by branch, you can use Rebase, and if you want to change the status to the previous state without changing the history, you can use Undo.

Reset

This is a command to return to a specific commit, such as canceling or deleting previous commits. Depending on the option, you can decide whether to keep the work or not.

  • Options include soft, mixed(defalut), and hard. I mainly use soft to check all changes at a specific point in time and maintain staging.

Revert

By undoing a specific commit and recording it as a new commit, the undo details of the previous commit are added to the commit history. Meanwhile, you can see that the commits are maintained. If you are collaborating, it is recommended to use Revert to avoid conflicts due to shared commits.

Rebase

This is a command that allows you to change or organize based on another branch. Rebase also allows intermediate commits to be deleted without history using ``pickanddrop```.

  • The type of Interactive Rebase can be checked with git rebase -i.
  • Squash: Used to combine several consecutive commits into one commit. Conversely, if you want to revert, you must cancel with ``git reset HEAD^ --hard``` and then commit again.
  • Fixup: Similar to Squash, but a new commit message cannot be specified and the previous commit message is used.
  • Reorder: Change commit order
  • Exec: This allows you to perform specific tasks between specific commits rather than changing the commit history. Allows temporary use of running certain scripts between commits, for example the following command:
pick c0ffee1 First commit
exec ./myscript.sh
pick 1a2b3c4 Second commit

<img src="/assets/post_images/git/git-rebase.png>

Undo

This refers to reverting changes in the working directory and staging area to the previous commit state. It does not affect commit history.

git reset [commit hash] --hard
git revert [commit hash]
git rebase [different branch]
git rebase -i HEAD~[number of commits]
git restore --source=[commit hash] --worktree --staged --source=HEAD [file name]

Reflecting the latest status of the original repository locally

It is used to retrieve the latest commit state from open source and update the existing local branch state. Here, upstream means the remote branch that I forked. Conversely, my repo brought through fork becomes the origin. In the code below, since it is a repo brought by git clone, upstream is added here and the latest status of the remote repository is retrieved through fetch. If there are two or more remote repositories, you can specify upstream separately, but if you want to reflect the latest information in all remote repositories, you can apply it with git fetch --all```. Next, the current branch commit is uploaded to upstream/main. If a conflict occurs at this time, you can resolve it and proceed with git rebase --continue. And apply it to local branch main```.

git remote add upstream [original repository url]
git fetch upstream
git checkout main
git rebase upstream/main
git push origin main --force

Add remote

If you need more than one remote branch, you can add one more remote so that when you commit, it can be applied or pulled to the corresponding remote. As explained earlier, there is a difference between origin and upstream, so they need to be used separately.

git remote add upstream [original repository url]
git remote add origin [original repository url]

Registered remote repositories can be checked with ``git remote -v```.


Comments

No comments yet. Be the first!