Giter VIP home page Giter VIP logo

git-study's People

Contributors

atlaschiew avatar atlaschiew3 avatar

Watchers

 avatar  avatar

git-study's Issues

git branch vs git switch

The git checkout command creates, and switches branches, restores and undo changes from commit; however, git switch only creates and switches branches. Git switch and git checkout let you create and move into a branch.

git stash

Stash is just a convenience method. Since branches are so cheap and easy to manage in git, I personally almost always prefer creating a new temporary branch than stashing, but it's a matter of taste mostly.

The one place I do like stashing is if I discover I forgot something in my last commit and have already started working on the next one in the same branch:

# Assume the latest commit was already done
# start working on the next patch, and discovered I was missing something

# stash away the current mess I made
git stash save

# some changes in the working dir

# and now add them to the last commit:
git add -u
git commit --amend

# back to work!
git stash pop

how do I create a remote Git branch?

First, create a new local branch and check it out:

git checkout -b <branch-name>

The remote branch is automatically created when you push it to the remote server:

git push <remote-name> <branch-name> 

is typically origin, which is the name which git gives to the remote you cloned from. Your colleagues may then simply pull that branch.

Note however that formally, the format is:

git push <remote-name> <local-branch-name>:<remote-branch-name>

But when you omit one, it assumes both branch names are the same. Having said this, as a word of caution, do not make the critical mistake of specifying only : (with the colon), or the remote branch will be deleted!

git reset vs git revert

It's important to understand that git revert undoes a single commit—it does not "revert" back to the previous state of a project by removing all subsequent commits. In Git, this is actually called a reset, not a revert.

git pull

git pull will merge the conflicted files into ur local copy. For safety, we can use git fetch to check the change.

preview git pull

# preview git pull
git fetch -v
git log -p HEAD..origin/main
git diff HEAD..origin/main

# apply the changes by merge..
git merge origin/main

# .. or just pull the changes
git pull
git pull --rebase origin master

uncommit

If you know you want to use git reset, it still depends what you mean by "uncommit". If all you want to do is undo the act of committing, leaving everything else intact, use:

git reset --soft HEAD^

If you want to undo the act of committing and everything you'd staged, but leave the work tree (your files) intact:

git reset HEAD^

And if you actually want to completely undo it, throwing away all uncommitted changes, resetting everything to the previous commit (as the original question asked):

git reset --hard HEAD^

The original question also asked it's HEAD^ not HEAD. HEAD refers to the current commit - generally, the tip of the currently checked-out branch. The ^ is a notation which can be attached to any commit specifier, and means "the commit before". So, HEAD^ is the commit before the current one, just as master^ is the commit before the tip of the master branch.

Here's the portion of the git-rev-parse documentation describing all of the ways to specify commits (^ is just a basic one among many).

what the hell is HEAD & detached HEAD?

In Git, HEAD refers to the currently checked-out branch’s latest commit. However, in a detached HEAD state, the HEAD does not point to any branch, but a specific commit or the remote repository.

new repo or push to existing repo

#create a new repository on the command line
echo "# test" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/atlaschiew/git.git
git push -u origin main
#push an existing repository from the command line
git remote set-url origin https://github.com/atlaschiew/git.git
git branch -M main
git push -u origin main

what is origin?

You can have multiple remotes, each with a different name - such as the default "origin" through git remote add

Your question makes the assumption that you currently have the master branch checked out.

git push -u origin master Push the local branch named master to the "origin" remote as the branch named master. The -u flag tells local git to track that remote branch as upstream to your local branch.

git pull vs git pull --rebase vs git rebase

git pull --rebase is a shorthand for git fetch and then a plain git rebase, as opposed as to the default git merge. The practical difference is that applying only the latter would not fetch any new commits from your remote prior to rebasing your code on top of, as it would only take into account what your local repository's already aware of.

It's also worth mentioning that merging conflicts would appear in the same way as a regular git pull.

unstage

git restore --staged <file>

check list of branches

check remote branches

git branch -r

check local branches

git branch

check all branches

git branch -a

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.