Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a "local-only commit" in git?

Tags:

git

I'm using git, and I'd like to be able to create a commit which isn't synced with the remote repository. Such a commit would have to "float" atop all other commits in the local repository to avoid affecting the history. I could use such a commit to store local-specific changes (configuration changes, debugging flags, local workarounds, etc.).

Currently, I manually rebase when I commit to reorder the commit back to the top, and I push using HEAD^ to avoid pushing the local changes. I've also considered putting the changes in the stash, but that's less convenient because it precludes normal use of the stash. Another alternative is to simply leave all those local changes unstaged, and use git add -p every time I want to commit. However, with a large number of trivial local changes, that becomes a hassle.

Here's an example of my current workflow:

My repository initially looks like

A---B---C---F master 

where "F" is my floating commit.

I make a commit:

A---B---C---F---D master 

then git rebase -i HEAD~2 to reorder:

A---B---C---D---F master 

then git push remote HEAD~1... to push everything but the local F commit.

The change F contains changes to existing versioned files, and may contain an arbitrary amount of changes. (If I can make more than one commit "floating", that would be even better, since I could then separate my local changes).

like image 559
nneonneo Avatar asked Nov 07 '12 19:11

nneonneo


People also ask

How do I locally commit?

The "commit" command is used to save your changes to the local repository. Note that you have to explicitly tell Git which changes you want to include in a commit before running the "git commit" command. This means that a file won't be automatically included in the next commit just because it was changed.

How do I pull a specific commit in local?

If you want to bring that specific COMMIT_ID to your local branch, you may either use git-cherry-pick to bring only that commit over, or git-merge to bring all changes up to that commit to your local branch. You can find more information in the Official Documentation.

Can I commit just one file git?

Normally we commit to git, all files are going to git but in your scenario push only single file git. For this, you have to run specific command to push the only single file to git. Above all commands are related to push the only single file to git but sometimes we want to push only two files to git in a single commit.

How do I push local commits?

To push the commit from the local repo to your remote repositories, run git push -u remote-name branch-name where remote-name is the nickname the local repo uses for the remote repositories and branch-name is the name of the branch to push to the repository. You only have to use the -u option the first time you push.


1 Answers

How about putting those changes into a local branch which you rebase/merge regularly from your main dev branch? That way there wouldn't be any danger of committing them upstream.

like image 175
koljaTM Avatar answered Sep 17 '22 09:09

koljaTM